New Refcard: We've been eagerly preparing this week's refcard release for the past few months now, and finally the widely-requested Scala Refcard is here. Go download Scala: The Scalable JVM Language right now! --DZ Curators
In Scala though, type classes are possible and sometimes useful, but
using them is not quite as second nature as it is in Haskell or Clojure.
My gut feeling is that this has to do with Scalas syntax in a couple of
Having worked quite a lot with Clojure recently, and also starting a more serious effort of learning Haskell, the differences in syntax compared to Scala have gotten me thinking. Using type-classes in Haskell and their rough equivalents in clojure (protocols and multimethods) is second nature, you rarely even think about it in terms of "ooh, let me use a type class here". Furthermore it's natural in these languages to think in terms of type classes as one of the first go-to ways of doing things.
Firstly, creating type-classes is more "syntax heavy" in Scala compared to just going the traditional way of using traits and objects. To put it crudely, it's simply more typing than the alternatives.
More importantly though, I think Scalas OO heritage is even more to
blame for the infrequency with which type classes are used: the syntax
ever so slightly nudges you towards thinking in terms of "objects"
(traits, Scala objects) that you call things on as you often write myThingy.calculateSomething(arg1, arg2).
Contrast this to Clojure or Haskell, where more often than not, the function is the first thing from the left (with a few exceptions in Haskell, but with added keyboard tapping to achieve it). At least for me, thinking in terms of function first, followed by function arguments certainly nudges me more easily towards the use of type classes.
These are unlikely to be observations universally applicable to everyone, but I doubt that I am alone in finding these syntactical characteristics of Scala at times nudging me away from using type classes, and it is a bit of a shame, as type classes really are a wonderful way of structuring code in a very modular and re-usable fashion.