Type Class Polymorphism         In Scala         Mayank Bairagi    Sr. Software Consultant            Knoldus
PolymorphismSame Operation Working on different type ofvaluesType of Polymorphism 1. Parametric 2. ad hoc
Polymorphism By ●Overloading ●Inheritance ●Pattern Matching ●Trait/ Interface ●Type parameters and Generic types
Type ClassesType Classes are Introduce first in haskell languageScala Type Classes are Pattern instead of language feature...
OverLoadingcase class Book(title:String,author:String)case class Movie(title:String,director:String)object OverLoading {  ...
OverLoadingcase class Book(title:String,author:String)case class Movie(title:String,director:String)object OverLoading {  ...
Interfacetrait Serializable {  def serialize:String}class Book(title:String,author:String) extends Serializable{  override...
Problem With Interface We Have Coupling Problem Here , How each classis serialize , this information has to be in the clas...
Pattern Matchingobject Serialize {  def serialize(x:Any)  {    x match {      case b:Book => "Book(" +b.title+","+b.author...
Problem With Pattern Matching●   Both the Movie and Book classes are unaware how    actually there serialized.●   If I nee...
Type Classcase class Book(title:String,author:String)case class Movie(title:String,director:String)trait Serializable[T] {...
Type Class With Implicitcase class Book(author:String) extends Card[Book]case class Movie(director:String) extends Card[Mo...
Type Variances and Context Bound         Co variance +T        Contra variance -T
Thank you
Upcoming SlideShare
Loading in …5
×

Type class polymorphism

1,471 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,471
On SlideShare
0
From Embeds
0
Number of Embeds
824
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Type class polymorphism

  1. 1. Type Class Polymorphism In Scala Mayank Bairagi Sr. Software Consultant Knoldus
  2. 2. PolymorphismSame Operation Working on different type ofvaluesType of Polymorphism 1. Parametric 2. ad hoc
  3. 3. Polymorphism By ●Overloading ●Inheritance ●Pattern Matching ●Trait/ Interface ●Type parameters and Generic types
  4. 4. Type ClassesType Classes are Introduce first in haskell languageScala Type Classes are Pattern instead of language featureExample:scala.math.Numaric, scala.math.OrderingEvery Thing in Scalaz is Type class ( monoids, monads,applicative , functors )
  5. 5. OverLoadingcase 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. 6. OverLoadingcase 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. 7. Interfacetrait 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. 8. Problem With Interface We Have Coupling Problem Here , How each classis serialize , this information has to be in the class. In order to add more trait and override the methodsI need to have control on these classes. I should beallowed to view and modify the source code.
  9. 9. Pattern Matchingobject Serialize { def serialize(x:Any) { x match { case b:Book => "Book(" +b.title+","+b.author+")" case m:Movie => "Movie(" +m.title+","+m.director+")" } }}
  10. 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 its 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. 11. Type Classcase 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. 12. Type Class With Implicitcase 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. 13. Type Variances and Context Bound Co variance +T Contra variance -T
  14. 14. Thank you

×