Tutorial: Typeclasses in Scala with Dan Rosen

InfoQ · 44K views · 2012 · 29:57

Open on YouTube

Functional programmingSoftware architecture & designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Scala questions where behavior must vary by type and they want a typesafe typeclass approach instead of casting or pattern matching.

Recommended by 2 people across 2 different months, 2012 to 2016.

What it is

A roughly 30 minute InfoQ tutorial by Dan Rosen on the typeclass design pattern in Scala. It shows how implicits let you build modular, decoupled designs that are open to extension, borrowing the idea from Haskell. It assumes basic Scala, algebraic data types and implicits.

What people said

Recommends Dominic Bou-Samra · 2012-11 · Stack Overflow · on “Specialization of generic functions in Scala (or Java)”

Use a typeclass pattern. It has the advantage over the instanceOf checking (or pattern matching) of being typesafe. import java.nio.ByteBuffer trait BufferWriter[A] { def write(buffer: ByteBuffer, a: A) } class BuffPimp(buffer: ByteBuffer) { def writeData[A: BufferWriter](data: A) = { implicitly[BufferWriter[A]].write(buffer, data) } } object BuffPimp { implicit def intWriter = new BufferWriter[Int] { def write(buffer: ByteBuffer, a: Int) = buffer.putInt(a) } implicit def doubleWriter = new BufferWriter[Double] { def write(buffer: ByteBuffer, a: Double) = buffer.putDouble(a) } implicit def longWriter = new BufferWriter[Long] { def write(buffer: ByteBuffer, a: Long) = buffer.putLong(a) } impl

Recommends JRomero · 2016-07 · Stack Overflow · on “Scala Function to create a conversion function from a string to a matched value”

A very unethical solution would be to simply cast the instance to "assist" the compiler def converter[T](columnType: String): String => T = { columnType match { case "int" => (s: String) => s.toInt.asInstanceOf[T] case "double" => (s: String) => s.toDouble.asInstanceOf[T] case "string" => (s: String) => s.asInstanceOf[T] } } val stringConverter = converter[String]("string") val intConverter = converter[Int]("int") val doubleConverter = converter[Double](doubleConverter) stringConverter("hello") intConverter("1") doubleConverter("3.00") A cleaner yet still unorthodox solution would be to use the type class to determine the type. def converter[T : Manifest]: String => T = { implicitly[Manifest

Cites Jens Schauder · 2012-10 · Stack Overflow · on “Scala case class inheritance”

case classes are perfect for value objects, i.e. objects that don't change any properties and can be compared with equals. But implementing equals in the presence of inheritance is rather complicated. Consider a two classes: class Point(x : Int, y : Int) and class ColoredPoint( x : Int, y : Int, c : Color) extends Point So according to the definition the ColorPoint(1,4,red) should be equal to the Point(1,4) they are the same Point after all. So ColorPoint(1,4,blue) should also be equal to Point(1,4), right? But of course ColorPoint(1,4,red) should not equal ColorPoint(1,4,blue), because they have different colors. There you go, one basic property of the equality relation is broken. update Yo