Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Lab 08: Polymorphism ======= ===== 8.1. Ad-hoc polymorphism ===== Consider the following implementation of the type ''Nat'', which you are well-familiar with: <code scala> trait Nat { def +(other: Nat): Nat } case object Zero extends Nat{ override def +(other: Nat): Nat = other } case class Succ(n: Nat) extends Nat{ override def +(other: Nat): Nat = Succ(n + other) } </code> It might be the case that we would like to use nat values from different sources, e.g. ''fromInteger(1) + Zero + fromString("2")''. Keeping track of all different functions that convert other values to Nat might be tedious. A work-around is to: **(1) define a companion object for Nat** by creating a new object with the same name as ''Nat'', in the same scala file (or worksheet). <code scala> object Nat { // a companion object for trait Nat } </code> We can now populate the companion objects with any methods we would like. In particular, we can add a method: <code> def apply(i: Int): Nat = ??? </code> which can be called simply as: ''Nat(5)''. Also, we can ** (2) overload** ''apply'' in order to support creating naturals with the call ''Nat(.)'' from different other types. **8.1.** Create a companion object for ''Nat'' and add appropriate apply functions in order to make the expression ''Nat(1) + Zero + Nat("2")'' successfully compile and run. ===== 8.2. Dictionaries =====