2016-09-24 43 views
3

在Swift 2.3中数组是否定义了元素关联类型?Array在哪里定义元素关联类型?

它必须定义它,因为它实现了具有元素和关联类型的GeneratorType。 Dictionary和Set都定义了元素,但是Array在哪里定义它?

数组有一个称为元素的泛型类型,具有泛型类型是否满足GeneratorType协议?

我在操场上试过这个,它对我没有帮助。

例如

protocol Animal{ 
    associatedtype Breed 
} 

struct Dog<Breed>: Animal{ 

} 

回答

0

在夫特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 
}