Patterns for Working With Associated Types

Method-Only Types

released Fri, 01 Mar 2019
Swift Version 5.0

Method-Only Types

If your associated type requirement doesn't come from Equatable conformance but instead from your own use, you can double-check if you actually need these associated types.

Take this example of a validator type:

protocol Validator {

     associatedtype I

     func validate(_ input: I) -> Bool

}

As the associated type is only used in one method, you can alternatively just make it a generic method and thus save yourself from introducing unnecessary unfinished types:

protocol Validator {

     func validate<I>(_ input: I) -> Bool

}