Ralph
Ralph

Reputation: 32284

Difference between definterface and defprotocol in Clojure

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

Upvotes: 17

Views: 2288

Answers (2)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

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.

Upvotes: 5

Julien Chastang
Julien Chastang

Reputation: 17774

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.

Upvotes: 12

Related Questions