What does "most specialized constraints" mean?

Hi,


I'm reading the latest Swift book (Swift 3.0.1) and I came across this sentence, from the bottom of: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html


If a conforming type satisfies the requirements for multiple constrained extensions that provide implementations for the same method or property, Swift will use the implementation corresponding to the most specialized constraints.


How can you decide which implementation will be chosen? That seems undetermined.


TIA

Mark

Replies

"most specialized constraints" should be a reference to the type hierarchy. A constraint defined on (or as an extension for) a subclass is more specialized than a constraint defined on (or as an extension for) the subclass's parent class.

From the docs, assuming an 'argument' clause is same as a constraint, in your example.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html


Generic Argument Clause

A generic argument clause specifies the type arguments of a generic type. A generic argument clause is enclosed in angle brackets (<>) and has the following form:

<generic argument list>

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.