Fork me on GitHub
#architecture
<
2021-02-02
>
richiardiandrea02:02:43

talking about protocols, is :extend-with-meta the only way to attach an implementation to a particular object instance?

phronmophobic02:02:42

it's not quite attaching to a particular object, but depending on the use case, proxy or reify may work for you

richiardiandrea02:02:31

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 InputStreams

seancorfield02:02:13

Since it requires metadata, you can't add it to a Java object.

seancorfield02:02:56

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?

phronmophobic02:02:26

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.

richiardiandrea02:02:55

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?

richiardiandrea03:02:01

actually it should be using with-meta but I wanted to add that ad-hoc if it makes sense

seancorfield03:02:59

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...

seancorfield03:02:03

(with-meta (constantly is) {'clojure.java.jdbc/set-parameter (fn [v stmt ix] (.setBinaryWhatever stmt (v) ix))}) something like that?

richiardiandrea03:02:43

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

seancorfield03:02:37

Yes, the protocol must be :extend-via-metadata true for you to extend it via metadata 🙂

seancorfield03:02:45

Which means you can't do that with clojure.java.jdbc as it stands today, sorry.

seancorfield03:02:44

The protocols in next.jdbc can nearly all be extended via metadata tho'...

seancorfield03:02:08

@U0C8489U6 I guess I could make a new version of clojure.java.jdbc for you that makes some of the protocols extensible via metadata?

seancorfield04:02:57

OK, 0.7.12 is heading to Maven Central with those protocols now extensible via metadata. Just for you!

metal 2
richiardiandrea04:02:55

woooooah whaat 😄

richiardiandrea04:02:54

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!

1