Frills and thrills – decorators again
In the last chapter, we looked at the decorator pattern and Scala's stackable modifications. Here is another way to use expression decorators. As we have had some curry, let's round it out with ice creams! Quite a feast there is today!
As if an ice cream itself is not enough a temptation, we have toppings as well. We will have nuts, jelly, and honey toppings. Feeling sinful already? Here comes the Scala version:
import scala.language.implicitConversions object IceCreams extends App { sealed trait IceCreamType { // 1 def price: Double } case object Vanilla extends IceCreamType { // 2 val price = 10.0 } case object Mango extends IceCreamType { val price = 20.0 } implicit def iceCreamPriceWrapper(iceCreamType: IceCreamType): Double = iceCreamType.price // 3 case class IceCream(price: Double) // 4 def add(c: IceCream)(p: Double) = c.copy(price = c.price + p) // 5 def addNuts(c: IceCream) = add(c)(15) // 6 def addJelly(c: IceCream...