Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between definterface and defprotocol in Clojure

Tags:

clojure

Other than lack of documentation, what is the difference between definterface and defprotocol in Clojure?

like image 971
Ralph Avatar asked Oct 14 '11 15:10

Ralph


2 Answers

According to the Joy of Clojure:

The advantages of using definterface over defprotocol are restricted entirely to the fact that the former allows primitive types for arguments and returns. At some point in the future, the same advantage will likely be extended to the interfaces generated [by protocols], so use definterface sparingly and prefer protocols unless absolutely necessary.

like image 50
Julien Chastang Avatar answered Nov 19 '22 06:11

Julien Chastang


My possibly incomplete understanding was definterface produces an interface .class that java code can implement in order to create classes suitable to pass to your Clojure functions.

Protocols are, in short, a faster and more focused way of doing dispatch than multimethods. you actually have running code in a protocol that is used by other clojure code.

like image 5
Arthur Ulfeldt Avatar answered Nov 19 '22 06:11

Arthur Ulfeldt