Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Protocols are just a faster, less general version of multimethods. They dispatch only on the type of the first parameter, and they do it quickly.

This speed is important because it allows the java interfaces that define the core constructs of Clojure (such as clojure.lang.IFn, clojure.lang.Seqable, etc.) to be defined purely in terms of protocols, thus bypassing the need for Java altogether. Using multimethods would have been untenably slow.

Protocols can also be useful in that they recognize that certain behavior is defined by a set of functions, rather than just one. Consider the Cantor library (http://github.com/ztellman/cantor), which does simple floating point math. Using only multimethods, it's possible that you could define addition for a type, but not subtraction. With protocols, it's much more difficult to make that mistake.



Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: