SlideShare a Scribd company logo
SHAPELESSSHAPELESS
Generic programming for
Scala !
Generic programming for
Scala !
Deepti Bhardwaj
Trainee Software Consultant
Knoldus Software LLP
What is Shapeless?
● Type class
● Dependent type based generic programming
library for Scala
● 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 example
def findSize(s: String): Int = s.length
monomorphic("foo")
// Polymorphic method example
def 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 value
heterogenous 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 Repr
def to(t:T) :Repr
def 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!
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-https://github.com/milessabin/shapeless/wiki/Feature-
Demo
https://github.com/knoldus/shapeless-demo
Shapeless- Generic programming for Scala
Shapeless- Generic programming for Scala

More Related Content

Shapeless- Generic programming for Scala

  • 1. SHAPELESSSHAPELESS Generic programming for Scala ! Generic programming for Scala ! Deepti Bhardwaj Trainee Software Consultant Knoldus Software LLP
  • 2. What is Shapeless? ● Type class ● Dependent type based generic programming library for Scala ● Type class ● Dependent type based generic programming library for Scala
  • 3. 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.
  • 4. Using Shapeless: To include it in your SBT build you should add: scalaVersion := "2.11.7" libraryDependencies ++= Seq( "com.chuusai" %% "shapeless" % "2.3.0")
  • 5. 1)Polymorphic function values ● Ordinary Scala function values are monomorphic. Shapeless, however, provides an encoding of polymorphic function values.
  • 6. // Monomorphic method example def findSize(s: String): Int = s.length monomorphic("foo") // Polymorphic method example def findSize[T](l: List[T]): Int = l.length findSize(List(1, 2, 3)) findSize(List("foo", "bar", "baz"))
  • 7. 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.
  • 8. 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
  • 9. 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.
  • 10. 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.
  • 11. 2)Heterogenous lists ● It has a map operation, applying a polymorphic function value across its elements. ● It also has a flatMap operation.
  • 12. ● 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
  • 13. 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
  • 14. monomorphic Scala map => monomorphic function value heterogenous shapeless map => polymorphic function value
  • 15. 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.
  • 16. 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 Repr def to(t:T) :Repr def from(r:Repr) :T }
  • 17. 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 !!
  • 18. 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! 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!
  • 19. 6)LabelledGeneric ● a case class can be represented generically as a record of its component fields — known as the “labelled generic representation”