Skip to content

Instantly share code, notes, and snippets.

@MaximilianoFelice
MaximilianoFelice / builder-phantom-types.scala
Last active July 26, 2023 22:49
A Builder example in Scala using Phantom Types
case class Food(ingredients: Seq[String])
class Chef[Pizza <: Chef.Pizza] protected (ingredients: Seq[String]) {
import Chef.Pizza._
def addCheese(cheeseType: String): Chef[Pizza with Cheese] = Chef(ingredients :+ cheeseType)
def addTopping(toppingType: String): Chef[Pizza with Topping] = Chef(ingredients :+ toppingType)
def addDough: Chef[Pizza with Dough] = Chef(ingredients :+ "dough")