Created
September 14, 2017 23:06
-
-
Save n4to4/93b864a09f61d81a0e8296cf43491f7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| object Main extends App { | |
| case class Food(ingredients: Seq[String]) | |
| class Chef[Pizza <: Chef.Pizza](ingredients: Seq[String] = Seq()) { | |
| import Chef.Pizza._ | |
| def addCheese(cheeseType: String): Chef[Pizza with Cheese] = new Chef(ingredients :+ cheeseType) | |
| def addTopping(toppingType: String): Chef[Pizza with Topping] = new Chef(ingredients :+ toppingType) | |
| def addDough: Chef[Pizza with Dough] = new Chef(ingredients :+ "dough") | |
| def build(implicit ev: Pizza =:= FullPizza): Food = Food(ingredients) | |
| } | |
| object Chef { | |
| def newChef = new Chef[Pizza.EmptyPizza]() | |
| sealed trait Pizza | |
| object Pizza { | |
| sealed trait EmptyPizza extends Pizza | |
| sealed trait Cheese extends Pizza | |
| sealed trait Topping extends Pizza | |
| sealed trait Dough extends Pizza | |
| type FullPizza = EmptyPizza with Cheese with Topping with Dough | |
| } | |
| } | |
| val c = | |
| Chef.newChef | |
| .addCheese("mozzarella") | |
| .addDough | |
| val p = | |
| c.addTopping("olives") | |
| .build | |
| println(p) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment