Type ClassesWealthfront Engineering
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
pol•y•mor•phism            ¦ päli môr fizəm¦nounthe occurrence of something in several  different forms	                   ...
In particular:Polymorphism is a programming language  feature that allows values of different data  types to be handled us...
Several Types of Polymorphism•    Ad-hoc polymorphism•    Inclusion polymorphism•    Parametric polymorphism•    Bounded p...
Several Types of Polymorphism•  Ad-hoc polymorphism  –  Operator and method overloading•  Inclusion polymorphism•  Paramet...
Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism  –  Subtyping•  Parametric polymorphism•  Bou...
Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism•  Parametric polymorphism  –  Generics•  Boun...
Several Types of Polymorphism•    Ad-hoc polymorphism•    Inclusion polymorphism•    Parametric polymorphism•    Bounded p...
Ad-hoc polymorphismControl moving through one named function is dispatched to various other functions without having to sp...
Ad-hoc polymorphismString greet() {   return "Hello, World!";}
Ad-hoc polymorphismString greet(String name) {   return "Hello," + name + "!";}
Ad-hoc polymorphismString greet(int count) {   return "Hello, World" + repeat("!", count);}
Ad-hoc polymorphism>   greet();    Hello, World!>   greet("Wealthfront");    Hello, Wealthfront!>   greet(3);    Hello, Wo...
Parametric polymorphismA function or a data type can be written  generically so that it can handle values  identically wit...
Parametric polymorphismsize :: a . [a] → intsize [] = 0size (x:xs) = 1 + size xs
Parametric polymorphism>   size [1, 2]    2>   size ["hello", "hi", "yo"]    3
Bounded parametric polymorphismA function or a data type require some  knowledge of its data types but can otherwise  work...
Bounded parametric polymorphism<T extends Comparable<T>> List<T> sort(List<T> list) {   return …}
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in par...
Type Class Eqclass Eq a where eq :: a → a → bool ne :: a → a → bool
Implementation for intinstance Eq int where eq m n = m == n ne m n = m != n
Implementation for Instrumentinstance Eq Instrument where eq x {id = xid} y {id = yid} = xid == yid ne x {id = xid} y {id ...
Usagemember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
Ad-hoc polymorphismmember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
Parametric polymorphismmember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
Constraint on type variablemember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
Type Classes vs. Interfaces?Similar, yet different:•  Implementation is separate•  Subtyping is not entailed•  Static disp...
Return type-based dispatchfoo :: (Parseable a) ⇒ a → String → afoo x s = x + parse s
Return type-based dispatch>   foo "Hello, " "World!"    Hello, World!>   foo 2 "3"    5
Return type-based dispatchclass String implements Parseable {   String parse() {      return this;   }   int parse() {    ...
Type Class in Scalatrait Parseable[T] {   def parse(s: String): T}
Implementation in Scala for Intimplicit object ParseableInt extends Parseable[Int] {   def parse(s: String) = Integer.pars...
Usage in Scaladef foo[T: Parseable](t: T, s: String): T =   t + implicitly[Parseable[T]].parse(s)
Usage in Scala>   foo("Hello, ", "World!");    Hello, World! : String>   foo(2, "3");    5 : Int
Usage in Scaladef foo[T: Parseable](s: String): T =   implicitly[Parseable[T]].parse(s)
Usage in Scala>   "Hello, " + foo("World!");    Hello, World! : String>   2 + foo("3");    5 : Int>   foo[Int]("3");    3 ...
The EndFYI, Monad is a Type Class.
Upcoming SlideShare
Loading in …5
×

Type Classes

501 views

Published on

A gentle introduction to Type Classes made for an internal Wealthfront Tech Talk.

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
501
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Type Classes

  1. 1. Type ClassesWealthfront Engineering
  2. 2. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  3. 3. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  4. 4. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  5. 5. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  6. 6. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  7. 7. pol•y•mor•phism ¦ päli môr fizəm¦nounthe occurrence of something in several different forms   New Oxford American Dictionary
  8. 8. In particular:Polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface
  9. 9. Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism•  Parametric polymorphism•  Bounded parametric polymorphism
  10. 10. Several Types of Polymorphism•  Ad-hoc polymorphism –  Operator and method overloading•  Inclusion polymorphism•  Parametric polymorphism•  Bounded parametric polymorphism
  11. 11. Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism –  Subtyping•  Parametric polymorphism•  Bounded parametric polymorphism
  12. 12. Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism•  Parametric polymorphism –  Generics•  Bounded parametric polymorphism
  13. 13. Several Types of Polymorphism•  Ad-hoc polymorphism•  Inclusion polymorphism•  Parametric polymorphism•  Bounded parametric polymorphism –  Generics with bounded wildcards
  14. 14. Ad-hoc polymorphismControl moving through one named function is dispatched to various other functions without having to specify the exact function being called
  15. 15. Ad-hoc polymorphismString greet() { return "Hello, World!";}
  16. 16. Ad-hoc polymorphismString greet(String name) { return "Hello," + name + "!";}
  17. 17. Ad-hoc polymorphismString greet(int count) { return "Hello, World" + repeat("!", count);}
  18. 18. Ad-hoc polymorphism> greet(); Hello, World!> greet("Wealthfront"); Hello, Wealthfront!> greet(3); Hello, World!!!
  19. 19. Parametric polymorphismA function or a data type can be written generically so that it can handle values identically without depending on their type
  20. 20. Parametric polymorphismsize :: a . [a] → intsize [] = 0size (x:xs) = 1 + size xs
  21. 21. Parametric polymorphism> size [1, 2] 2> size ["hello", "hi", "yo"] 3
  22. 22. Bounded parametric polymorphismA function or a data type require some knowledge of its data types but can otherwise work parametrically
  23. 23. Bounded parametric polymorphism<T extends Comparable<T>> List<T> sort(List<T> list) { return …}
  24. 24. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  25. 25. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  26. 26. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  27. 27. Type ClassType system construct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  28. 28. Type Class Eqclass Eq a where eq :: a → a → bool ne :: a → a → bool
  29. 29. Implementation for intinstance Eq int where eq m n = m == n ne m n = m != n
  30. 30. Implementation for Instrumentinstance Eq Instrument where eq x {id = xid} y {id = yid} = xid == yid ne x {id = xid} y {id = yid} = xid != yid
  31. 31. Usagemember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
  32. 32. Ad-hoc polymorphismmember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
  33. 33. Parametric polymorphismmember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
  34. 34. Constraint on type variablemember :: (Eq a) ⇒ a → [a] → boolmember y [] = falsemember y (x:xs) = (eq x y) || member y xs
  35. 35. Type Classes vs. Interfaces?Similar, yet different:•  Implementation is separate•  Subtyping is not entailed•  Static dispatch (ad-hoc polymorphism) vs. dynamic single dispatch
  36. 36. Return type-based dispatchfoo :: (Parseable a) ⇒ a → String → afoo x s = x + parse s
  37. 37. Return type-based dispatch> foo "Hello, " "World!" Hello, World!> foo 2 "3" 5
  38. 38. Return type-based dispatchclass String implements Parseable { String parse() { return this; } int parse() { return Integer.parseInt(this); }}
  39. 39. Type Class in Scalatrait Parseable[T] { def parse(s: String): T}
  40. 40. Implementation in Scala for Intimplicit object ParseableInt extends Parseable[Int] { def parse(s: String) = Integer.parseInt(s)}
  41. 41. Usage in Scaladef foo[T: Parseable](t: T, s: String): T = t + implicitly[Parseable[T]].parse(s)
  42. 42. Usage in Scala> foo("Hello, ", "World!"); Hello, World! : String> foo(2, "3"); 5 : Int
  43. 43. Usage in Scaladef foo[T: Parseable](s: String): T = implicitly[Parseable[T]].parse(s)
  44. 44. Usage in Scala> "Hello, " + foo("World!"); Hello, World! : String> 2 + foo("3"); 5 : Int> foo[Int]("3"); 3 : Int
  45. 45. The EndFYI, Monad is a Type Class.

×