Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Last active December 3, 2015 14:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoSoto/90db7a1adad7cd9cb3c6 to your computer and use it in GitHub Desktop.
Save NachoSoto/90db7a1adad7cd9cb3c6 to your computer and use it in GitHub Desktop.
Type-erased ValidatorType
protocol ValidatorType {
typealias ValidatedType
func isValid(object: ValidatedType) -> Bool
}
// You might want to make it a reference type, depending on your case.
struct AnyValidator<T>: ValidatorType {
private let validatorBlock: (T) -> Bool
init<V: ValidatorType where V.ValidatedType == T>(validator: V) {
self.init(validatorBlock: validator.isValid)
}
private init(validatorBlock: (T) -> Bool) {
self.validatorBlock = validatorBlock
}
func isValid(object: T) -> Bool {
return self.validatorBlock(object)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment