O'Reilly logo

Scala Functional Programming Patterns by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required