talking about protocols, is :extend-with-meta
the only way to attach an implementation to a particular object instance?
it's not quite attaching to a particular object, but depending on the use case, proxy
or reify
may work for you
yeah my use case was to attach the ISQLParameter
protocol impl to an instance of InputStream
I did not want to extend all of the InputStream
s
Since it requires metadata, you can't add it to a Java object.
Extending protocols with metadata works for Clojure data but not Java data. So in that case you are either going to have to extend ISQLParameter
to all InputStream
's or you're going to need some sort of explicit wrapper around InputStream
perhaps?
it seems like you could use proxy
for this use case to wrap the input stream and also implement ISQLParameter
. not sure it would be a good idea.
yeah the wrapper is what I went for anyways, but I was naively thinking that I could just
(assoc my-wrapper-m 'clojure.java.jdbc/set-parameter (fn [] ...))
Am I completely off here?
actually it should be using with-meta
but I wanted to add that ad-hoc if it makes sense
Yeah, you can attach metadata to a function, and you could use (constantly my-input-stream)
and then have the set-parameter
implementation call the value that is passed in and then read the data from that I guess...
(with-meta (constantly is) {'clojure.java.jdbc/set-parameter (fn [v stmt ix] (.setBinaryWhatever stmt (v) ix))})
something like that?
I think that would work, but do we need :extend-via-metadata true
on the protocol also?
I have actually opened a support issue today for that
Yes, the protocol must be :extend-via-metadata true
for you to extend it via metadata 🙂
Which means you can't do that with clojure.java.jdbc
as it stands today, sorry.
The protocols in next.jdbc
can nearly all be extended via metadata tho'...
@richiardiandrea I guess I could make a new version of clojure.java.jdbc
for you that makes some of the protocols extensible via metadata?
OK, 0.7.12 is heading to Maven Central with those protocols now extensible via metadata. Just for you!
woooooah whaat 😄
I was planning to patch tomorrow lol - I know we are in the same time zone so I have no other words than a big Thank you!