Shapeless- Generic programming for Scala

download Shapeless- Generic programming for Scala

If you can't read please download the document

Transcript of Shapeless- Generic programming for Scala

SHAPELESS

Generic programming for Scala !

Deepti BhardwajTrainee Software ConsultantKnoldus Software LLP

What is Shapeless?

Type class

Dependent type based generic programming library for Scala

Why Shapeless ?

Shapeless is about programming with types.

Doing things at compile-time that would more commonly be done at runtime to ensure type-safety and effectiveness.

Using Shapeless:

To include it in your SBT build you should add:scalaVersion := "2.11.7"

libraryDependencies ++= Seq( "com.chuusai" %% "shapeless" % "2.3.0")

1)Polymorphic function values

Ordinary Scala function values are monomorphic. Shapeless, however, provides an encoding of polymorphic function values.

// Monomorphic method exampledef findSize(s: String): Int = s.lengthmonomorphic("foo")

// Polymorphic method exampledef findSize[T](l: List[T]): Int = l.length

findSize(List(1, 2, 3))findSize(List("foo", "bar", "baz"))

Defining polymorphic function values:

The parametric polymorphism is moved to the method inside the object.

They are able to capture type-specific cases.

Being polymorphic, they may be passed as arguments to functions or methods and then applied to values of different types within those functions.

HList Vs List

HLists are lists of objects of arbitrary types, where the type information for each object is kept. In fact, in Scala, we may do:

import shapeless._val l = 10 :: "string" :: 1.0 :: Nil

but the type of l then would be List[Any], because the common super type of the elements there would be, in fact, only Any. A HList is declared similarly to a List:

val hl = 10 :: "string" :: 1.0 :: HNil

except for the terminator HNil. But the type of hl is actually Int :: String :: Double :: HNil.

HList vs Tuple

The benefit of using HLists instead of tuples is that they can be used in all those situations where a tuple would work just as well, but without the 22-elements limit.Also, shapeless allows standard Scala tuples to be manipulated in exactly the same ways as HLists.

2)Heterogenous lists

It has a map operation, applying a polymorphic function value across its elements.

It also has a flatMap operation.

It has a set of fully polymorphic fold operations which take a polymorphic binary function value. The fold is sensitive to the static types of all of the elements of the Hlist

3)Heterogenous maps

Shapeless provides a heterogenous map which supports an arbitrary relation between the key type and the corresponding value type,

class BiMapIS[K, V]implicit val intToString = new BiMapIS[Int,String]implicit val stringToInt = new BiMapIS[String, Int]//this implies the map thus declared can have int as key and String as value and vice - versa

monomorphic Scala map => monomorphic function valueheterogenous shapeless map => polymorphic function value

4)Coproduct

a generalization of Scala's Either to an arbitrary number of choices

allows you to put together more than two types.

Either with more than two possible types.

Coproducts are mutually exclusive, only one of the elements is going to be present at runtime.

5)Generic

Simply put, a case class can be represented generically as an HList of its component types known as the generic representation

Converting a case class to/from its generic representation is accomplished by using Generic!trait Generic[T] {type Reprdef to(t:T) :Reprdef from(r:Repr) :T }

case class Intern(name: String, email: String,id :Int, address:String)case class Employee(name: String, email: String,id :Int, address:String)

we want to construct Employee from Intern but want to do it automatically without passing all parameters. Generic is used here !!

In this case, Employee and Intern are nearly identical. What if the Employee had an extra property or a less property?

The solution is provided by LabelledGeneric!

6)LabelledGeneric

a case class can be represented generically as a record of its component fields known as the labelled generic representation

References

https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0

Demo

https://github.com/knoldus/shapeless-demo