Richard Searle


A change in typing philosophy, indicated by Scala type classes

06 Sep 2010

This discussion provides a clear and useful discussion of how Scala type classes might be used. The code requires that a type be specified, which then indirectly defines the code to be executed
val i = foo[Int]("123") // 123 val f = foo[Float]("123.0") // 123.0
Where i and f will have types Int and Float respectively. Coming from other strongly typed languages, one might be tempted to write
val i:Int = foo("123") // 123 val f:Float = foo("123.0") // 123.0
Which does not work. It might be argued that the type inferencer should be sophisticated enough to map this code to the earlier form. Ignoring any technical reasons that prohibit the second form (or would lead to undesirable ambiguities), there are other reasons for prefer the first style.