Archives for the tag: scala

Scala: Types of a higher kind

One of the more powerful features Scala has is the ability to generically abstract across things that take type parameters. This feature is known as Higher Kinded Types (HKT). This feature allows us to write a library that works with a much wider array of classes, whereas without the feature you are condemned to bespoke and error ridden code duplication for each class that may want the functionality. Type constructors Essentially what HKT gives us is the ability to generalize across type

Continue reading »

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 »