SlideShare a Scribd company logo
1 of 14
Type Class Polymorphism
         In Scala



         Mayank Bairagi
    Sr. Software Consultant
            Knoldus
Polymorphism
Same Operation Working on different type of
values


Type of Polymorphism
 1. Parametric

 2. ad hoc
Polymorphism By
 ●Overloading
 ●Inheritance

 ●Pattern Matching

 ●Trait/ Interface

 ●Type parameters and Generic types
Type Classes

Type Classes are Introduce first in haskell language

Scala Type Classes are Pattern instead of language feature

Example:
scala.math.Numaric, scala.math.Ordering

Every Thing in Scalaz is Type class ( monoids, monads,
applicative , functors )
OverLoading

case class Book(title:String,author:String)
case class Movie(title:String,director:String)

object OverLoading {
  def serialize(book:Book)= "Book("
+book.title+","+book.author+")"

  def serialize(movie:Movie)= "Book("
+movie.title+","+movie.director+")"
}
OverLoading

case class Book(title:String,author:String)
case class Movie(title:String,director:String)

object OverLoading {
  def serialize(book:Book)= "Book("
+book.title+","+book.author+")"

  def serialize(movie:Movie)= "Book("
+movie.title+","+movie.director+")"
}
Interface
trait Serializable {
  def serialize:String
}

class Book(title:String,author:String) extends Serializable{
  override def serialize= "Book(" +this.title+","+this.author+")"
}

class Movie(title:String,director:String) extends Serializable{
  override def serialize= "Movie("
+this.title+","+this.director+")"
}
Problem With Interface

 We Have Coupling Problem Here , How each class
is serialize , this information has to be in the class.
 In order to add more trait and override the methods
I need to have control on these classes. I should be
allowed to view and modify the source code.
Pattern Matching

object Serialize {
  def serialize(x:Any)
  {
    x match {
      case b:Book => "Book(" +b.title+","+b.author+")"
      case m:Movie => "Movie(" +m.title+","+m.director+")"
    }
  }
}
Problem With Pattern Matching
●   Both the Movie and Book classes are unaware how
    actually there serialized.
●   If I need more than one type of serialization than I need
    more serialize method with it's own case match block.
●   Now we have fixed the coupling problem, but
    unfortunatly we have introduce new coupling
●   Method serialize need to know about all the classes
    which need to be serialize.
●   Problem of control and source code is still exist
Type Class
case class Book(title:String,author:String)
case class Movie(title:String,director:String)

trait Serializable[T] {
  def ser(t:T):String
}

object Serializable{
  def serialize[T](t:T, s:Serializable[T])=s.ser(t)
}

object BookIsSerialzabel extends Serializable[Book]
{
  def ser(book:Book)= "Book(" +book.title+","+book.author+")"
}

object MovieIsSerialzabel extends Serializable[Movie]
{
  def ser(movie:Movie)= "Movie(" +movie.title+","+movie.director+")"
}
Type Class With Implicit
case class Book(author:String) extends Card[Book]
case class Movie(director:String) extends Card[Movie]

trait Serializable[T] {
  def ser(t:T):String=t.asInstanceOf[Card[T]].title
}

object Serializable{
 def serialize[T](t:T)(implicit s:Serializable[T])=s.ser(t)

 implicit object BookIsSerialzabel extends Serializable[Book]
  {override def ser(book:Book)= "Book(" +book.title+","+book.author+")"}

  implicit object MovieIsSerialzabel extends Serializable[Movie]
   { override def ser(movie:Movie)= "Movie("
+movie.title+","+movie.director+")"}
}
Type Variances and Context Bound




         Co variance +T
        Contra variance -T
Thank you

More Related Content

Similar to Type class polymorphism

Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
shinolajla
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
scala reloaded
scala reloadedscala reloaded
scala reloaded
Mac Liaw
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 

Similar to Type class polymorphism (20)

scala-101
scala-101scala-101
scala-101
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Java/Scala Lab 2016. Руслан Шевченко: Несколько трюков scala-разработки, приг...
Java/Scala Lab 2016. Руслан Шевченко: Несколько трюков scala-разработки, приг...Java/Scala Lab 2016. Руслан Шевченко: Несколько трюков scala-разработки, приг...
Java/Scala Lab 2016. Руслан Шевченко: Несколько трюков scala-разработки, приг...
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach us
 
Object Class
Object Class Object Class
Object Class
 
scala reloaded
scala reloadedscala reloaded
scala reloaded
 
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorial
 
Real world gobbledygook
Real world gobbledygookReal world gobbledygook
Real world gobbledygook
 
4Developers: Paweł Szulc- Real-World Gobbledygook
4Developers: Paweł Szulc- Real-World Gobbledygook 4Developers: Paweł Szulc- Real-World Gobbledygook
4Developers: Paweł Szulc- Real-World Gobbledygook
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Python to scala
Python to scalaPython to scala
Python to scala
 

Type class polymorphism

  • 1. Type Class Polymorphism In Scala Mayank Bairagi Sr. Software Consultant Knoldus
  • 2. Polymorphism Same Operation Working on different type of values Type of Polymorphism 1. Parametric 2. ad hoc
  • 3. Polymorphism By ●Overloading ●Inheritance ●Pattern Matching ●Trait/ Interface ●Type parameters and Generic types
  • 4. Type Classes Type Classes are Introduce first in haskell language Scala Type Classes are Pattern instead of language feature Example: scala.math.Numaric, scala.math.Ordering Every Thing in Scalaz is Type class ( monoids, monads, applicative , functors )
  • 5. OverLoading case class Book(title:String,author:String) case class Movie(title:String,director:String) object OverLoading { def serialize(book:Book)= "Book(" +book.title+","+book.author+")" def serialize(movie:Movie)= "Book(" +movie.title+","+movie.director+")" }
  • 6. OverLoading case class Book(title:String,author:String) case class Movie(title:String,director:String) object OverLoading { def serialize(book:Book)= "Book(" +book.title+","+book.author+")" def serialize(movie:Movie)= "Book(" +movie.title+","+movie.director+")" }
  • 7. Interface trait Serializable { def serialize:String } class Book(title:String,author:String) extends Serializable{ override def serialize= "Book(" +this.title+","+this.author+")" } class Movie(title:String,director:String) extends Serializable{ override def serialize= "Movie(" +this.title+","+this.director+")" }
  • 8. Problem With Interface We Have Coupling Problem Here , How each class is serialize , this information has to be in the class. In order to add more trait and override the methods I need to have control on these classes. I should be allowed to view and modify the source code.
  • 9. Pattern Matching object Serialize { def serialize(x:Any) { x match { case b:Book => "Book(" +b.title+","+b.author+")" case m:Movie => "Movie(" +m.title+","+m.director+")" } } }
  • 10. Problem With Pattern Matching ● Both the Movie and Book classes are unaware how actually there serialized. ● If I need more than one type of serialization than I need more serialize method with it's own case match block. ● Now we have fixed the coupling problem, but unfortunatly we have introduce new coupling ● Method serialize need to know about all the classes which need to be serialize. ● Problem of control and source code is still exist
  • 11. Type Class case class Book(title:String,author:String) case class Movie(title:String,director:String) trait Serializable[T] { def ser(t:T):String } object Serializable{ def serialize[T](t:T, s:Serializable[T])=s.ser(t) } object BookIsSerialzabel extends Serializable[Book] { def ser(book:Book)= "Book(" +book.title+","+book.author+")" } object MovieIsSerialzabel extends Serializable[Movie] { def ser(movie:Movie)= "Movie(" +movie.title+","+movie.director+")" }
  • 12. Type Class With Implicit case class Book(author:String) extends Card[Book] case class Movie(director:String) extends Card[Movie] trait Serializable[T] { def ser(t:T):String=t.asInstanceOf[Card[T]].title } object Serializable{ def serialize[T](t:T)(implicit s:Serializable[T])=s.ser(t) implicit object BookIsSerialzabel extends Serializable[Book] {override def ser(book:Book)= "Book(" +book.title+","+book.author+")"} implicit object MovieIsSerialzabel extends Serializable[Movie] { override def ser(movie:Movie)= "Movie(" +movie.title+","+movie.director+")"} }
  • 13. Type Variances and Context Bound Co variance +T Contra variance -T