你的论点有缺陷.Kotlin还没有发布*,锡兰刚刚发布了它的第一个版本,我将引用它在《their announcement》中缺失的一点:
So, excuse me, but what implementation proves it is possible? In fact, I haven't looked much at what Kotlin is promising, but what Ceylon is promising is just what manifests already provide, but in a transparent manner.
But let's consider the problem you described in your question:
trait Handles[E <: Event] {
def handle(event: E)
}
所以,首先,JVM不提供任何方法来识别接口或类中的类型参数,所以JVM可以判断这些参数.但是,您可以在实现Handles
的每个对象中存储关于E
代表什么的信息,就像您可以在Scala中编写的那样:
abstract class Handles[E <: Event : Manifest] {
def handle(event: E)
}
Next, let's see the method handle
. Again, JVM provides no way of using type parameters in a method definition. The only way to implement that is to have handle
accept Object
as parameter: ie, type erasure.
And here's the deal: to make handle
callable from Java, it must be type erased. And, if it is type erased, then it is subject to the limitation described in your question. The only way to get around that is to drop Java compatibility (which, by the way, is not available in Ceylon's first release either).
是的,根据马丁·奥德斯基(Martin Odersky)的说法,Scala将在2.10上进行materialized (某种形式).但无论它提供什么(我打赌更透明地使用 list 来声明类型平等),这个特定的限制是JVM固有的,如果不放弃Java集成,就无法克服.
(*) Kotlin has a demo out now, and its reification -- so far -- is just a syntactic sugar for bundling manifests and instanceOf tests. It's still subject to all the same limitations Scala is.