What is a type class and why do I need it?
Type classes are useful to solve several fundamental challenges in programming languages. In particular, type classes support retroactive extension: the ability to extend existing software modules with new functionality without needing to touch or re-compile the original source.
The Scala library includes a few typeclasses such as scala.math.Numeric and scala.math.Ordering, and Scalaz is all typeclasses.
Scala enables the typeclass pattern using traits and implicits, and whilst Scala’s implementation is more verbose than Haskell’s, it comes with greater flexibility. Haskell only allows a single typeclass instance globally, whereas Scala allows any number to be available.
Furthermore, Scala allows default implementations to be made available if no others can be found.
For a deeper understanding see the references at the bottom of the blog.
Example
(You can copy and run this code in your favorite IDE like IntelliJ/Eclipse ....)
package test.home
/*
* The trait defines a concept which in this case is a transformer that transforms a type T into a R.
*/
trait Transformer[T, R] {
def transform(t: T): R
}
/*
* This is a companion object for the typeclass giving default implementations for the typeclass.
* These implementations are found after local implicits, so you can still override the default
* behaviour. For more about the search order see [3].
*/
object Transformer {
implicit object IntToStringTransformer extends Transformer[Int, String] {
def transform(t: Int) = {
println("I m in IntToStringTransfer");
t.toString
}
}
}
// This is something that makes use of the typeclass
trait Transform {
// The implicit Transformer, transformer, supplies an appropriate transformer for the method
def transformTrait[T, R](t: T)(implicit transformer: Transformer[T, R]): R = {
println("I m in transformTrait()");
transformer.transform(t)
}
}
// These examples will drop back to the default implementations in the Transformer's companion object
object ExampleWithDefaults extends App with Transform {
println(transformTrait(5))
}
//Output would be
I m in transform()
I m in IntToStringTransfer
5
Hope I am able to resolve your confusion about typeclass in scala
Type classes are useful to solve several fundamental challenges in programming languages. In particular, type classes support retroactive extension: the ability to extend existing software modules with new functionality without needing to touch or re-compile the original source.
The Scala library includes a few typeclasses such as scala.math.Numeric and scala.math.Ordering, and Scalaz is all typeclasses.
Scala enables the typeclass pattern using traits and implicits, and whilst Scala’s implementation is more verbose than Haskell’s, it comes with greater flexibility. Haskell only allows a single typeclass instance globally, whereas Scala allows any number to be available.
Furthermore, Scala allows default implementations to be made available if no others can be found.
For a deeper understanding see the references at the bottom of the blog.
Example
(You can copy and run this code in your favorite IDE like IntelliJ/Eclipse ....)
package test.home
/*
* The trait defines a concept which in this case is a transformer that transforms a type T into a R.
*/
trait Transformer[T, R] {
def transform(t: T): R
}
/*
* This is a companion object for the typeclass giving default implementations for the typeclass.
* These implementations are found after local implicits, so you can still override the default
* behaviour. For more about the search order see [3].
*/
object Transformer {
implicit object IntToStringTransformer extends Transformer[Int, String] {
def transform(t: Int) = {
println("I m in IntToStringTransfer");
t.toString
}
}
}
// This is something that makes use of the typeclass
trait Transform {
// The implicit Transformer, transformer, supplies an appropriate transformer for the method
def transformTrait[T, R](t: T)(implicit transformer: Transformer[T, R]): R = {
println("I m in transformTrait()");
transformer.transform(t)
}
}
// These examples will drop back to the default implementations in the Transformer's companion object
object ExampleWithDefaults extends App with Transform {
println(transformTrait(5))
}
//Output would be
I m in transform()
I m in IntToStringTransfer
5
Hope I am able to resolve your confusion about typeclass in scala
No comments:
Post a Comment