clojure-dev

Issues: https://clojure.atlassian.net/browse/CLJ | Guide: https://insideclojure.org/2015/05/01/contributing-clojure/
2020-01-14T05:35:56.043600Z

So in some ways, a Clojure protocol is like a Java interface. One difference that I think is true is: a Java interface can extend another Java interface, but there is no notion of a Clojure protocol extending another Clojure protocol. Is that correct?

2020-01-14T05:40:44.046600Z

Of course another difference being that a developer can make a type extend a protocol, even if they did not write the code that defines the type, without modifying that code. But you cannot make a Java class implement a Java interface directly except by modifying the source code defining that Java class. (there are techniques like delegation in Java that let you create 'shim code' that achieves a somewhat similar effect, I think, but is a bit more tedious to write the code for than something like Clojure's extend-type)

bronsa 2020-01-14T09:43:25.046900Z

but there is no notion of a Clojure protocol extending another Clojure protocol. Is that correct eeeh technically one can, ish

bronsa 2020-01-14T09:44:59.047300Z

user=> (defprotocol P (f [_]))
P
user=> (defprotocol Q (g [_]))
Q
user=> (extend-type user.Q P (f [this] (g this)))
nil
user=> (deftype T [] Q (g [_] :g))
user.T
user=> (f (T.))
:g

🤯 1
2020-01-14T10:26:00.048600Z

That is taking advantage of the fact that Clojure/Java creates a Java interface user.Q, and so this is a trick specific to Clojure/Java, it appears?

bronsa 2020-01-14T10:37:15.048800Z

yes

bronsa 2020-01-14T10:37:45.049Z

not using any implementation details/undefined behavior mind you

bronsa 2020-01-14T10:37:59.049400Z

extending a protocol to an interface is supported, and so is the underlying interface of a protocol

🦜 1
alexmiller 2020-01-14T13:28:21.050400Z

I would say you probably shouldn’t do that

alexmiller 2020-01-14T13:29:00.051Z

There is another technique though that I’ve used

bronsa 2020-01-14T13:29:31.052100Z

extending to object with a runtime extend?

alexmiller 2020-01-14T13:29:41.052300Z

Yeah

bronsa 2020-01-14T13:29:51.052700Z

yeah that's a bit better

alexmiller 2020-01-14T13:30:13.053500Z

Which was suggested by Rich at the first conj during Chousers protocol talk

alexmiller 2020-01-14T13:30:37.054100Z

I have a fuller example of that in Clojure Applied