[英]Swift Generic Constraints on Types
是否可以定義一個通用參數都符合某個給定類型的類?
例如
protocol Foo{}
class Bar : Foo {}
class Baz : Foo {}
class Qux<T, U, V where U: T, V: T> {}
let qux = Qux<Foo, Bar, Baz>()
不,不是一種類型。 類型約束指定類型參數必須從類繼承,或符合協議或協議組合:
class SomeClass<TypeParameter: ProtocolOrClass>
你為什么不這樣做:
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.