Skip to content

Instantly share code, notes, and snippets.

@nlinker
Created December 9, 2012 07:25
Show Gist options
  • Save nlinker/4243761 to your computer and use it in GitHub Desktop.
Save nlinker/4243761 to your computer and use it in GitHub Desktop.
Scala cake pattern
abstract class Cake {
def top: String
def middle: String
def bottom: String
override def toString = top
}
trait Cherry { this: Cake =>
def top = "cherry on top of " + middle
}
trait Cream { this: Cake =>
def middle = "creamy " + bottom
}
trait Biscuit { this: Cake =>
def bottom = "biscuit"
}
(new Cake with Cherry with Cream with Biscuit).toString // “cherry on top of creamy biscuit”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment