Created
December 9, 2012 07:25
-
-
Save nlinker/4243761 to your computer and use it in GitHub Desktop.
Scala cake pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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