4

What feature of what language does Clojure Protocols derive from?

Note: this is not a question about "How to Use Protocols"

This is a question about what inspired the idea.

In particular, what I want is to get something like:

  • XYZ + ABC inspired Clojure Protocols

Then, I can go off and read about directions people went with XYZ and ABC for language features.

Thanks!

Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150

2 Answers2

7

If you know Java, Clojure protocols are similar to Java interfaces - but better. The Clojure website says this about protocols (and there's more background information and rationale about protocols on that page):

  • Provide a high-performance, dynamic polymorphism construct as an alternative to interfaces
  • Support the best parts of interfaces
    • specification only, no implementation
    • a single type can implement multiple protocols
  • While avoiding some of the drawbacks
    • Which interfaces are implemented is a design-time choice of the type author, cannot be extended later (although interface injection might eventually address this)
    • implementing an interface creates an isa/instanceof type relationship and hierarchy
  • Avoid the 'expression problem' by allowing independent extension of the set of types, protocols, and implementations of protocols on types, by different parties
    • do so without wrappers/adapters
  • Support the 90% case of multimethods (single dispatch on type) while providing higher-level abstraction/organization

Furthermore, you might find Solving the Expression Problem with Clojure 1.2 an interesting read:

Clojure expert Stuart Sierra introduces you to new features in Clojure 1.2 that solve the Expression Problem, a classic programming dilemma. Protocols let you extend preexisting types to new methods, and datatypes let you extend preexisting methods to new types — all without changing the existing code. You'll also see how Java™ interfaces and classes can interact with Clojure protocols and datatypes.

Gert
  • 3,839
  • 19
  • 22
  • 2
    Note that Java interfaces, in turn, are directly taken from Objective-C protocols (Objective-C was *the* main influence on the design of Java). Objective-C protocols, then, are inspired by the idea of protocols in Smalltalk, which in Smalltalk are purely a documentation convention, which Objective-C turned into a language feature. – Jörg W Mittag May 11 '15 at 22:12
3

I don't know, was Clojure Protocols inspired by something, except desire to solve Expression Problem, but Protocols are doing similar thing as Haskell typeclasses, for example...

Look to this question for more information - it also mentions Scala traits

Community
  • 1
  • 1
Alex Ott
  • 80,552
  • 8
  • 87
  • 132