Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created September 14, 2017 23:06
Show Gist options
  • Select an option

  • Save n4to4/93b864a09f61d81a0e8296cf43491f7b to your computer and use it in GitHub Desktop.

Select an option

Save n4to4/93b864a09f61d81a0e8296cf43491f7b to your computer and use it in GitHub Desktop.
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