Skip to content

Instantly share code, notes, and snippets.

Last active December 3, 2015 14:21
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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