Created
January 12, 2019 12:52
-
-
Save xplosunn/913de8171f6007532c24e982822f537a to your computer and use it in GitHub Desktop.
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
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