8. www.exoplatform.com Copyright 2011 eXo Platform 1 st Unification ADTs are class hierarchies Many functional languages have algebraic data types and pattern matching =>Concise and canonical manipulation of data structure From object-oriented point of view -”ADTs are not extensible” - “ADTs violate the purity of the OO data model” - “Pattern matching breaks encapsulation” => using class hierarchies.
9. www.exoplatform.com Copyright 2011 eXo Platform 1 st Unification ADTs are class hierarchies With class hierarchies contents is encapsulaated in the object and accessed through methods With ADTs : data is accessed through decomposing the value by pattern matching ADTs and class hierarchies have complementary strength and weakness - ADTs allow easy extension of operations supported by the data - While class hierarchies allow easy addition of data variants
10. www.exoplatform.com Copyright 2011 eXo Platform 1 st Unification ADTs are class hierarchies ADTs can be encoded using case classes - Case classes are like normal classes. - Instance constructors can be recovered by pattern matching - Structural equality is used for comparison - The new keyword is optional for instance creation. Case class ClockTime(hour: Int, min:Int) is a valid case class definition. ClockTime(10,30) creates an instance