简体   繁体   English

关于类型的Swift通用约束

[英]Swift Generic Constraints on Types

Is it possible to define a class whose generic parameters both conform to some given type? 是否可以定义一个通用参数都符合某个给定类型的类?

For example 例如

protocol Foo{}
class Bar : Foo {}
class Baz : Foo {}
class Qux<T, U, V where U: T, V: T> {}

let qux = Qux<Foo, Bar, Baz>()

No. Not to a type. 不,不是一种类型。 Type constraints specify that a type parameter must inherit from a class, or conform to a protocol or protocol composition: 类型约束指定类型参数必须从类继承,或符合协议或协议组合:

class SomeClass<TypeParameter: ProtocolOrClass>

Why don't you just do: 你为什么不这样做:

protocol Foo {}
class Bar: Foo {}
class Baz: Foo {}
class Qux<U: Foo, V: Foo> {}

let qux = Qux<Bar, Baz>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM