问题描述
我试图从通用类派生我的类:
class foo< T& {}
class bar:foo< Int> {}
但是此代码无法使用en错误进行编译:
如何避免此限制?可能吗?
Ssreg,
a href =https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/GenericParametersAndArguments.html>官方:
让我们希望苹果在未来的版本中修复这个问题。
同时,让我们看到这是一个利用而不是子类。
注意:
作为一个穷人版本的解决方案,可以使用 typealias
:
class foo< T> {}
class bar< Int> :foo< Int> {}
typealias Bar = bar< Int>
这样,剩下的代码可以写成苹果已经解决的问题。 / p>
I'm trying to derive my class from generic class:
class foo<T> {}
class bar : foo<Int> {}
But this code fails to compile with en error:
How to avoid this limitation? Is it possible?
Ssreg,
Unfortunately this is official:
Let us hope Apple fixes this in a future version.
Meanwhile, let us see this as an opportunity to exploit aggregation instead of subclassing.
NOTE:
As a poor man's version of a solution, one could use typealias
:
class foo<T> {}
class bar<Int> : foo<Int> {}
typealias Bar = bar<Int>
This way, the rest of the code can be written just as if Apple already fixed the matter.
这篇关于在swift中从泛型类派生的类的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!