Skip to content

Instantly share code, notes, and snippets.

@xplosunn
Created January 12, 2019 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xplosunn/913de8171f6007532c24e982822f537a to your computer and use it in GitHub Desktop.
Save xplosunn/913de8171f6007532c24e982822f537a to your computer and use it in GitHub Desktop.
import PersonBuilder._
import shapeless._
case class Person(name: String, age: Int)
object PersonBuilder {
def apply() = new PersonBuilder[Start](Person("", 0))
trait Start
trait StepName
trait StepAge
type AllSteps = StepAge with StepName
}
class PersonBuilder[StepsDone] private (person: Person) {
def name(name: String)(implicit ev: StepsDone <:!< StepName) = {
new PersonBuilder[StepsDone with StepName](person.copy(name = name))
}
def age(age: Int)(implicit ev: StepsDone <:!< StepAge) = {
new PersonBuilder[StepsDone with StepAge](person.copy(age = age))
}
def build()(implicit ev: StepsDone <:< AllSteps): Person = {
person
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment