Skip to content

Instantly share code, notes, and snippets.

@jonasabreu
Created January 30, 2014 17:55
Show Gist options
  • Save jonasabreu/8714602 to your computer and use it in GitHub Desktop.
Save jonasabreu/8714602 to your computer and use it in GitHub Desktop.
package aux
class BuilderTypeSafe {
type Sim
type Nao
class Pessoa
class Builder[TemNome, TemEndereco] {
def comNome(nome : String) = new Builder[Sim, TemEndereco]
def comEndereco(nome : String) = new Builder[TemNome, Sim]
}
object Builder {
def apply() = new Builder[Nao, Nao]
}
implicit class AddBuild(builder : Builder[Sim, Sim]) {
def build = new Pessoa
}
Builder().build // não compila
Builder().comNome("").build // nem isso
Builder().comEndereco("").build // ou isso
Builder().comNome("").comEndereco("").build
Builder().comEndereco("").comNome("").build
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment