func foo<T: SomeClass>(object: T) -> Array<T> func foo<T>(object: T) -> Array<T> where T: SomeClass
解决方法
The requirements in a generic where clause specify that a type
parameter inherits from a class or conforms to a protocol or protocol
composition. Although the generic where clause provides syntactic
sugar for expressing simple constraints on type parameters (for
instance,<T: Comparable>
is equivalent to<T> where T: Comparable
and
so on),you can use it to provide more complex constraints on type
parameters and their associated types. For instance,you can constrain
the associated types of type parameters to conform to protocols. For
example,<S: Sequence> where S.Iterator.Element: Equatable
specifies
thatS
conforms to theSequence
protocol and that the associated typeS.Iterator.Element
conforms to theEquatable
protocol. This constraint
ensures that each element of the sequence is equatable.
简单地说,在其中允许您指定关于通用参数的关联类型的约束,而在<>中你不能这样做.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。