Hacker News new | past | comments | ask | show | jobs | submit login

why not just program to protocols?

For complex data structures I typically just hide details behind a protocol and deal with the set of interface functions.

You can then freely mess with the internals and not worry about details






I don't know enough about protocols. Will the compiler stop you if you misuse them?

The compiler will not know if a protocol is not passed to a function expecting a protocol. Whereas a static typing language will not compile.

Similarly nothing prevents invoking missing functions of a protocol, you will only know during runtime.


Well you're typically not thinking of a compilation step with Clojure.. so I found the original question a bit not-applicable

The goal isn't to introduce .. a compilation step? but to have the program blow up in the spot where there is an type mismatch. If you don't use a protocol you may not blow up, you may generate a nil, and you may blow up much further down the line (or not at all)

In the rare instances where dynamic types cause problems, they're virtually always something convoluted like that. The protocol design pattern describes the interface and protects you from hard to debug situations


In my experience (mainly when trying to understand the implementation of core.logic), the problem with protocols is that the code inspection tools choke on them.



Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: