Ok, you might be right after all :(...

I called :impls on the protocol and it returns a map from Class => impls-map. So my understanding was not not quite right...Yes, you can extend the same protocol in multiple namespaces, but for *distinct* types only! So basically in my case, i need to copy/paste the protocol in each impl namespace separately.

Many thanks for your insight Carlo :)


On 22/11/2019 21:27, dimitris wrote:

Hi Carlo thanks for you reply...

Quoted from clojure.org/reference/protocols:

/Avoid the 'expression problem' by allowing independent extension of the set of types, protocols, and implementations of protocols on types, by different parties./

What you said about protocol extensions being global directly contradicts the above. You can definitely extend the same protocol on the same type in multiple namespaces (i.e. independent extension by different parties). If extensions were global that would be no different than monkey-patching, right?


Now, you ask why I need the protocol in the first place. That's to provide polymorphism for bytes/chars/String input. The multi-method is there to provide polymorphism for all the different hashing implementations (bcrypt/scrypt/pbkdf2/argon2). These two are completely separate and shouldn't interfere.


On 22/11/2019 21:06, Carlo Zancanaro wrote:
Hey Dimitris,

I think what you're running into here is a fundamental misunderstanding of protocols. Specifically, this is not true:

As I understand it, protocol extensions are namespace-specific.

In Clojure, protocol extensions are global. If you extend a protocol for a type then it doesn't matter which namespace calls the protocol function, it only matters what type the function is called on. Protocols are approximately interfaces in Java, except they can have implementations added to existing types at runtime.

With your current code, I am a little bit confused why you want to use a protocol. You have a multimethod which performs dispatch based on a key, but which then calls the same protocol function for each dispatch value - why not just call the appropriate function in the right namespace? The protocol seems to be attempting to add a second level of indirection, but it's not clear to me how or why that is useful.

Carlo

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/9d8b69c8-c7c1-f982-140b-748df9b40ddf%40gmail.com.

Reply via email to