在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
总结: 1、类型约束只能添加到泛型参量上面 2、关联类型是泛型参量; 3、关联类型可以通过 协议.关联类型名称的形式引用;
4、约束的语法有两种:1)继承类语法 2)where语句语法; 继承类语法是约束语法的简化形式,适用于继承和符合语句的形式: public struct Tuple<A: Equatable, B: Equatable> 直接把约束添加到类型参量的声明中;
where语句形式的约束有两重作用: 1)增加可读性;将约束从主声明中剥离; 2)添加非继承形式的约束和复杂的约束功能 where T == Rule.RowValueType associatedtype Iterator: IteratorProtocol where Iterator.Element == Item public extension Request where Response == Void public extension Thenable where T: Sequence, T.Iterator.Element: Comparable
https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553 'where' clause cannot be attached to a non-generic declaration Type Constraints Type constraints specify that a type parameter must inherit from a specific class, or conform to a particular protocol or protocol composition.
For example, Swift’s Dictionary type places a limitation on the types that can be used as keys for a dictionary. As described in Dictionaries, the type of a dictionary’s keys must be hashable. That is, it must provide a way to make itself uniquely representable. Dictionary needs its keys to be hashable so that it can check whether it already contains a value for a particular key. Without this requirement, Dictionary could not tell whether it should insert or replace a value for a particular key, nor would it be able to find a value for a given key that is already in the dictionary.
public struct Dictionary<Key, Value> where Key : Hashable
protocol ComparableContainer: Container where Item: Comparable { }
public struct Tuple<A: Equatable, B: Equatable> {
public let a: A public let b: B
public init(a: A, b: B) { self.a = a self.b = b }
} protocol ComparableContainer: Container where Item: Comparable { }
protocol BatchedCollectionType: Collection { associatedtype Base: Collection }
|
请发表评论