Created
February 11, 2016 15:43
-
-
Save Shinpeim/14d0791f0719ec1de17d 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 scala.util.Try | |
import scala.language.reflectiveCalls | |
trait Validator[T] { | |
def apply[O <: T](obj: O): Try[O] | |
} | |
type Username = String | |
object UsernameSpecification { | |
type UsernameHolder = { val username: Username } | |
val validator: Validator[UsernameHolder] = new Validator[UsernameHolder] { | |
override def apply[O <: UsernameHolder](obj: O): Try[O] = Try { | |
val username = obj.username | |
// validate username... | |
obj | |
} | |
} | |
} | |
case class User(val username: Username) | |
case class Group(val name: String) | |
val usernameValidator = UsernameSpecification.validator | |
usernameValidator(User("takkkun")) // => OK | |
usernameValidator(Group("a group")) // completion error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment