89

In his talk Simple Made Easy, Rick Hickey talks about "Polymorphism a la carte" (about 30:00 into the video). In the same context, he also mentions Haskell's Type Classes and Clojure's Multi-Methods (and protocols).

Since I am not very familiar with these concepts, I would like to understand its usefulness when trying to achieve simplicity. I am particularly interested in any examples or showcases of this concept in Scala.

Grega Kešpret
  • 11,827
  • 6
  • 39
  • 44

1 Answers1

46

You can take Polymorphism a la carte as Polymorphism on demand.

Clojure community are proud of the term Polymorphism a la carte because of the fact that Clojure support multiple polymorphism strategies. Some of them are:

  • Prototype-based polymorphism

  • Inheritance polymorphism

    This is the polymorphism strategy used by Java. Clojure support this by proxy. Useful when doing Java interop.

  • Protocol

    Protocol to Clojure is as TypeClass to Haskell.

  • Multimethod

    While protocols provide polymorphic dispatch based on the type of the first argument, multimethods are much more flexible which can dispatch based on any function of the method's (any) arguments.

Polymorphism a la carte means "Select whatever polymorphism strategy best for your case. They're all in your toolbox."

You can implement TypeClass pattern in Scala using implicits. Read Scalaz source if you want real world examples. Scala does not support multimethods at language level, but I guess it is possible with the help of the upcoming 2.10 macro.

As for the benefits, advanced polymorphism strategies such as TypeClass and Multimethod can help solve the Expression Problem.

"The goal is to define a datatype by cases, where one can add new cases to the datatype and new functions over the datatype, without recompiling existing code, and while retaining static type safety (e.g., no casts)".

BTW, this question is too big to fit into a single StackOverflow question. My suggestion is to get familiar with these concepts, and then you'll understand their usefulness.

Community
  • 1
  • 1
xiefei
  • 6,563
  • 2
  • 26
  • 44
  • 1
    Very small nitpick, but if "protocols provide polymorphic dispatch based on the type of the first argument", then they don't correspond to Haskell type classes. Type class instances can "dispatch" on anything (you can even have zero-argument type functions returning a value in an instance of a type class like `read`, and the type the return value is *expected* to be will determine which instance is used). – Ben Nov 28 '12 at 22:43
  • 1
    `read` does take an argument, of course. Not sure where my brain was at when I wrote that. `mempty` is an example of what I was talking about. `read` does dispatch based on its expected return type, though. – Ben Nov 29 '12 at 23:42
  • " *[...] The goal is to define a datatype by cases, where one can add new cases to the datatype and new functions over the datatype [...]* ". ... What is meant with **case** here? – Abdull Apr 11 '13 at 10:19