Archives for the tag: scala

Covariance and Contravariance in Scala

I spent some time trying to figure out co- and contra-variance in Scala, and it turns out to be both interesting enough to be worth blogging about, and subtle enough that doing so will test my understanding! So, you've probably seen classes in Scala that look a bit like this: [cc escaped="true" lang='scala' ] sealed abstract class List[+A] { def head : A def ::[B >: A] (x:B) : List[B] = ... ... } [/cc] And you've probably heard that the +A means that A is a "covariant type

Continue reading »

Scala and Erasure

Mention generics to anyone who knows much about them and they'll usually have an opinion on type reification and erasure. For the uninitiated, erasure is where a List of Strings (or a List parameterised by the String type) "forgets" that it has been parameterised by the String type once it has been compiled – ie. the type parameter is erased. Reification is the opposite, the type is remembered, or reified. Different platforms have different strategies for their generics implementations.

Continue reading »