Last active
December 3, 2015 14:21
-
-
Save NachoSoto/90db7a1adad7cd9cb3c6 to your computer and use it in GitHub Desktop.
Type-erased ValidatorType
This file contains hidden or 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
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