Created
January 30, 2014 17:55
-
-
Save jonasabreu/8714602 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
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