Scala Type Levels

Scala Type Level – Part 4 : Way to the HigherKinds

Hello Everyone,
In this series we are having on the plate what we wish, with respect to Type Level. We have talked about the Upper Bounds, Inheritance, Self-Recursive and Implicit in details. Scala is more powerful when we talk about its Type Classes.

In this blog we will talk about the HigherKinds or we can say that import scala.language.higherKinds.

First of all, lets have a look at blogs published in the series so far,

Now Consider the following block of code,

case class Product[A, B, C[_]](id: A, price: B, content: C[_])

object HelloWorld {
   def main(args: Array[String]) {
      println(Product(1, 50, List("Jaipur")))
      println(Product(1, 50, List(1, 2)))
      println(Product(1, 50, Map("name" -> "Perfume")))
   }
}

Above code is simple and generates the output as of the case class object Product.
Below code is a quite different,

case class Product[A, B, C[_,_]](id: A, price: B, content: C[_,_])

object HelloWorld {
   def main(args: Array[String]) {
      println(Product(1, 50, Map("name" -> "Perfume")))
   }
}

If we see in above two blocks, there are two differences

  1. Definition of Type C is changed from C[_] to C[_,_],
  2. Way of calling remains same for Map statement and use of List removed.

Actually whenever we define a Type Class with two blank or partial arguments like C[_,_], it will point to the collection which takes exactly two types of arguments. In this case we use a Map.
Here Map[String,String] is similar to C[_,_].

Now, if we pass a list of numbers to Type C[_,_], lets see what comes in for,

case class Product[A, B, C[_,_]](id: A, price: B, content: C[_,_])

object HelloWorld {
   def main(args: Array[String]) {
      println(Product(1, 50.0, List(1,2)))
   }
}

It will result into the following error,


HelloWorld.scala:5: error: inferred kinds of the type arguments (Int,Double,scala.collection.LinearSeqOptimized) do not conform to the expected kinds of the type parameters (type A,type B,type C).
scala.collection.LinearSeqOptimized's type parameters do not match type C's expected parameters:
type Repr's bounds : Nothing <: Any
println(Product(1, 50.0, List(1,2)))
^
one error found

As a result to this error, We can use Scala Tuples too, because we can force exactly two arguments to pass in the case class.
Have a look at below code,

case class Product[A, B, C[_,_]](id: A, price: B, content: C[_,_])

object HelloWorld {
   def main(args: Array[String]) {
      println(Product(1, 50.0, ("Harsh", "Sharma")))
   }
}

Above code will also execute successfully,

Thats all for this blog, we will continue with the type levels in the next blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s