使用自我作为泛型类型 [英] Use Self as generic type
问题描述
<$ c $>可以使用Self >作为方法的返回类型:c> func doSomething() - > Self {}
是否有可能使用 Self
作为这样的泛型类型?
func doSomething() - >包装<自> {}
示例
如果我可以继承ChristmasPresent并让它有一个包装的
函数,该函数返回一个带有泛型集合的WrappedPresent,那么将会很好。
class ChristmasPresent {
func wrapped() - > WrappedPresent<自> {
return WrappedPresent(present:self)
}
}
class WrappedPresent< T:ChristmasPresent> {
var present:T
init(present:T){
self.present = present
}
}
class ToyCar:ChristmasPresent {}
let wrappedToyCar = ToyCar()。wrapped()//推断为:WrappedPresent< ToyCar>
Swift中最棘手的悖论是:Swift更喜欢方法,但Swift的功能更强大。 Swift团队知道,并且有一天我确信我们将拥有强大的方法。但今天不是那一天。有很多事情你想用你不能的方法来表达。然而,所有你想要的功能都可以轻松完成。
class ChristmasPresent {}
struct WrappedPresent< ; T:ChristmasPresent> {
let present:T
}
func wrap< T:ChristmasPresent>(present:T) - > WrappedPresent< T> {
return WrappedPresent(present:present);
}
class ToyCar:ChristmasPresent {}
let wrappedToyCar = wrap(ToyCar())//推断为:WrappedPresent< ToyCar>
请注意,如果您的代码完成了编译,您可能仍然很惊讶在结果。 Swift自定义类型不是协变,所以 WrappedPresent< ToyCar>
不是 WrappedPresent< ChristmasPresent>
的子类型。所以如果你有一堆包裹的礼物,你不能把一个包裹的玩具车放进去。这很容易迫使你回到仅仅使用固定的(非类型参数化的) WrappedPresent
类型,从而使问题没有意义。泛型和类并不总是和你在Swift中想象的一样。
如果你有一个你想要解决的问题的实际例子,那么我建议把它放在开发论坛上。 Swift团队的响应速度非常快。
Self
can be used as the return type of a method:
func doSomething() -> Self {}
Is it somehow possible to use Self
as a generic type like this?
func doSomething() -> Wrapper<Self> {}
Example
It would be nice if I could subclass ChristmasPresent and let it have a wrapped
function that returns a WrappedPresent with the generic set to whatever the subclass was.
class ChristmasPresent {
func wrapped() -> WrappedPresent<Self> {
return WrappedPresent(present: self)
}
}
class WrappedPresent<T: ChristmasPresent> {
var present: T
init(present: T) {
self.present = present
}
}
class ToyCar: ChristmasPresent {}
let wrappedToyCar = ToyCar().wrapped() // Inferred to be: WrappedPresent<ToyCar>
The most vexing paradox in Swift is this: "Swift prefers methods, but Swift's functions are more powerful." The Swift team knows that, and someday I am certain we will have powerful methods. But today is not that day. There are many things you'd like to express in methods that you cannot. Everything you want can be done easily with functions, however.
class ChristmasPresent {}
struct WrappedPresent<T: ChristmasPresent> {
let present: T
}
func wrap<T:ChristmasPresent>(present: T) -> WrappedPresent<T> {
return WrappedPresent(present: present);
}
class ToyCar: ChristmasPresent {}
let wrappedToyCar = wrap(ToyCar()) // Inferred to be: WrappedPresent<ToyCar>
Note that if your code did compile, you might still be quite surprised at the result. Swift custom types are not covariant, so a WrappedPresent<ToyCar>
is not a subtype of WrappedPresent<ChristmasPresent>
. So if you had an array of wrapped presents, you could not put a wrapped toycar in it. That could easily force you back to just using a fixed (non-type-parameterized) WrappedPresent
type anyway, making the question moot. Generics and classes do not always mix as well as you might imagine in Swift.
If you have a practical example of a problem you'd want to solve with this, then I do recommend bringing it up on the dev forums. The Swift team is very responsive.
这篇关于使用自我作为泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!