Poor’s mans Type Classes

Martin Odersky is back with more Scala, and he promises today will be a collection of all sorts of Scala features. First Martin will talk about Type Classes, since this was the topic of Simon Peyton Jones’s talk this morning.

In Haskell they work out very nice, so one could ask whether we should add them to OO languagues, like Scala. One problem is that the keywords Type and Class are already taken 🙂 Martin notes that Type Classes are just passed around dictionaries, so we just concentrate on this difference, this led to the concept of implicit parameters, that play the role of the passed around dictionaries, as shown in the figure below. If an implicit parameter is missing, it will be inferred.

(Very similar to the example Martin showed, but taken from http://www.scala-lang.org/node/114)

There is also the concept of conditional implicits, since implicit methods themselves can have implicit parameters.

External extensibility

In Scala, you can add new functionality to built-in type, by applying a trait to an existing type. An example is if we want to apply Ordered on Int or List. For this, we can also use implicit, which look something like this

(stolen from http://www.scala-lang.org/node/130)

To put things in some perspective compared to this mornings lecture, Martin states that the following analologies (more or less) hold between Haskell and Scala, while considering Type Classes:

  • type class = class
  • instance declaration = implicit conversion
  • dictionary = object
  • default method in class = concrete member
  • method signature in class = abstract member
Next up: a simple Scala example
Martin introduces an example about translating strings to phone number, by using the letters on a phone (2 = a,b,c 3 = d,e,f etc)
This example is taken from a paper by Prechelt: An empirical Comparison of Seven Programming Languages (IEEE = pay wall, can’t grab the paper now since I am not on the TU Delft network, might fix this later) that compares seven different implementations of this problem and compared them in terms of loc and speed. Sounds like an interesting paper!
In Scala, this example will look like this:
}
(taken from http://stackoverflow.com/questions/7156653/cant-seem-to-get-withdefaultvalue-to-work I love that Martin always used the same examples, makes live blogging a lot easier. Otherwise I would have to copy everything from the slides.
As you can see from this example, Scala is a very compact language, although maybe a bit cryptic fro novices. The withDefaultValue List() in the final function definition, for instance, makes sure that when inputting “1111” we do not get an exception (since 1 is not in the dictionary), but we get a nice empty list. Not immediately clear for me as non-Scala expert, but I can imagine this is nice once you get used to it.
Next up is the encode function, that returns all ways to encode a number as a list of words. This can be solved with a ‘divide-and-conquer technique’ that looks sort of intuitive in Scala, if you are more or less familiar with Haskell or other functional languages.
(taken from the same StackOverflow page)
A nice feature that I learned about from this example is that in Eclipse, in the same text file, you can declare an object of type coders, and try your code right there. You can ask function what its signature is (like in Haskell) or execute a method, like wordCode(“123”) and the results are shown next to the call. This makes it easy to do lightweight TDD and start with an object and try a method that you have not implemented yet. A bit easier that in VS, where you would always need a test class to do this. More Scala after the break.