在夫特2,Array
符合CollectionType
,它定义了要求:通过实现它像这样
/// Returns the element at the given `position`.
public subscript (position: Self.Index) -> Self.Generator.Element { get }
Array
满足这个要求:
public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer {
// ...
// Swift can infer that Self.Index == Int & Self.Generator.Element == Element
public subscript (index: Int) -> Element
// ...
}
这允许Swift t o推断Array
的通用占位符类型Element
用于满足关联类型Self.Generator.Element
,具体类型Int
用于满足关联类型Self.Index
。
这利用了关联类型可以通过在符合该协议的给定类型的协议要求的实现中使用的类型隐含地满足的事实。例如,你的情况:
protocol Animal {
associatedtype Breed
var breed : Breed { get }
}
// I've named the generic placeholder T to simply make clear that Swift is inferring
// T == Breed and therefore satisfying the associatedtype requirement.
// You can rename the generic placeholder back to 'Breed' – the compiler will still
// infer that it's satisfying the Breed associatedtype.
struct Dog<T> : Animal{
let breed : T
}
enum BreedOfCat {/* ... */}
struct Cat : Animal {
// same principle with non-generic types: BreedOfCat == Breed
let breed : BreedOfCat
}
虽然,当然,如果有使用associatedtype
没有协议要求,就必须满足明确将其typealias
:
protocol Animal {
associatedtype Breed
}
struct Dog<T> : Animal{
typealias Breed = T
}