在Scala 2.10中实现了泛化的泛型 [英] Reified generics in Scala 2.10

查看:126
本文介绍了在Scala 2.10中实现了泛化的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中缺少具体化的泛型是最让我误解的语言,因为

Kotlin和Ceylon都支持泛化的泛型,所以它绝对有可能在JVM之上这样做。在过去有人说,如果没有JVM的改变,Scala就无法支持它们,但现在Scala 2.10是相对简单的。 =nofollow noreferrer>传言对物化的支持有限。所以我的问题是:


  • 我们可以期望在Scala 2.10中实现物化,例如我能够实现一个通用性状多次?它是多么有限?

  • 如果Scala 2.10的物化结果比 Kotlin 锡兰。为什么会这样?


解决方案

您的论点有缺陷。 Kotlin尚未发布*,并且Ceylon刚刚发布了它的第一个版本,我会引用它缺少的一些内容他们的公告



  • 更新过的泛型


,但实现证明它有可能吗?事实上,我并没有多注意Kotlin的前景,但锡兰很有希望,正如已经提供的那样,但以透明的方式。



但是让我们考虑一下您在问题中描述的问题:

  trait Handles [E<:Event] { 
def handle(event:E)
}

所以,首先,JVM不提供任何标识接口或类中的类型参数的方法,所以JVM不能检查 E 。但是,您可以在每个实现 Handles 的对象中存储关于 E 所代表的信息,就像您可以编写这在Scala中:

 抽象类Handles [E<:Event:Manifest] {
def handle(event: E)
}

接下来,让我们看看 handle 。同样,JVM不提供在方法定义中使用类型参数的方法。实现这一点的唯一方法是将 handle 接受 Object 作为参数:例如,输入erasure。



这里的处理是:为了使Java中的 handle 可以被调用,它必须被擦除。而且,如果它被删除,那么它受限于你的问题中描述的限制。解决这个问题的唯一方法是降低Java的兼容性(顺便说一下,在Ceylon的第一个版本中也没有这个兼容性)。

是的,Scala将具有物化(某种)在2.10,根据马丁Odersky。但无论它提供了什么(并且我打赌更加透明地使用清单来声明类型相等),这个特殊的限制是JVM固有的,并且不会在不删除Java集成的情况下被克服。



(*)Kotlin现在已经有了一个演示,到目前为止它的具体化仅仅是一个捆绑清单和instanceOf测试的语法糖。它仍然受到Scala所有相同的限制。


The lack of reified generics in Scala is the thing that bugs me the most about the language, since simple things cannot be implemented without using complicated constructs.

Both Kotlin and Ceylon supports reified generics so its definitely possible to do so on top of the JVM. In the past it was said that Scala could not support them without a change in the JVM, but now Scala 2.10 is rumored to have limited support for reification. So my question is:

  • What can we expect for reification in Scala 2.10, will I for example be able to implement a generic trait multiple times ?. Just how limited is it ?
  • If Scala 2.10's reification turns out to be more limited than Kotlin and Ceylon. Why is that ?

解决方案

Your argument is flawed. Kotlin has not been released yet*, and Ceylon just had its first version released, and I'll quote one of the things it is missing from their announcement:

  • reified generics

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)
}

So, first of all, JVM doesn't provide any way of identifying type parameters in interfaces or classes, so E cannot be checked by JVM. You can, however, store information about what E stands for in each object that implements Handles, just like you could write this in 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).

Yes, Scala will have reification (of some sort) on 2.10, according to Martin Odersky. But whatever it provides (and I'm betting on more transparent use of manifests to assert type equality), this particular limitation is intrinsic to JVM and cannot be overcome without dropping Java integration.

(*) 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.

这篇关于在Scala 2.10中实现了泛化的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆