对于 Scala 来说,类型擦除有什么优势吗? [英] For Scala are there any advantages to type erasure?

查看:39
本文介绍了对于 Scala 来说,类型擦除有什么优势吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过很多关于不同 JVM 语言的消息,这些语言仍然处于雾件模式,建议以某种方式实现具体化.我有这种唠叨的回忆(或完全想象,不知道哪个)认为我在某处读到 Scala 以某种方式利用了 JVM 的类型擦除来做一些它无法通过具体化做的事情.这对我来说没有意义,因为 Scala 是在 CLR 和 JVM 上实现的,所以如果具体化导致某种限制,它会出现在 CLR 实现中(除非 CLR 上的 Scala 只是忽略具体化).

I've been hearing a lot about different JVM languages, still in vaporware mode, that propose to implement reification somehow. I have this nagging half-remembered (or wholly imagined, don't know which) thought that somewhere I read that Scala somehow took advantage of the JVM's type erasure to do things that it wouldn't be able to do with reification. Which doesn't really make sense to me since Scala is implemented on the CLR as well as on the JVM, so if reification caused some kind of limitation it would show up in the CLR implementation (unless Scala on the CLR is just ignoring reification).

那么,对于 Scala 来说,类型擦除是否有好的一面,或者具体化是一件绝对的好事?

So, is there a good side to type erasure for Scala, or is reification an unmitigated good thing?

推荐答案

参见 Ola Bini 的博客.众所周知,Java 具有使用站点协方差,在您认为合适的地方用很少的问号来实现.Scala 具有定义站点协方差,由类设计器实现.他说:

See Ola Bini's blog. As we all know, Java has use-site covariance, implemented by having little question marks wherever you think variance is appropriate. Scala has definition-site covariance, implemented by the class designer. He says:

泛型是一个复杂的语言特性.它变得更加添加到已有的语言时会变得复杂子类型.这两个功能在一般情况,在将它们添加到语.将它们添加到虚拟机很简单,如果该机器只需要提供一种语言 - 并且该语言使用相同的仿制药.但是泛型还没有完成.不完全理解如何正确处理,新的突破正在发生(Scala 是一个很好的例子).此时不能考虑泛型做得对".不只有一种类型的泛型 - 它们在实施策略、功能和极端案例.

Generics is a complicated language feature. It becomes even more complicated when added to an existing language that already has subtyping. These two features don’t play very well together in the general case, and great care has to be taken when adding them to a language. Adding them to a virtual machine is simple if that machine only has to serve one language - and that language uses the same generics. But generics isn’t done. It isn’t completely understood how to handle correctly and new breakthroughs are happening (Scala is a good example of this). At this point, generics can’t be considered "done right". There isn’t only one type of generics - they vary in implementation strategies, feature and corner cases.

...

这一切的意思是,如果你想将具体化的泛型添加到JVM,您应该非常确定该实现可以包含两者都是想要自己创新的静态语言泛型的版本,以及所有想要创建一个的动态语言良好的实现和与 Java 的良好接口设施图书馆.因为如果你添加不满足的具体泛型这些标准,你会扼杀创新并使它变得那么多难以将 JVM 用作多语言 VM.

What this all means is that if you want to add reified generics to the JVM, you should be very certain that that implementation can encompass both all static languages that want to do innovation in their own version of generics, and all dynamic languages that want to create a good implementation and a nice interfacing facility with Java libraries. Because if you add reified generics that doesn’t fulfill these criteria, you will stifle innovation and make it that much harder to use the JVM as a multi language VM.

即如果我们在 JVM 中对泛型进行了具体化,那么这些具体化的泛型很可能不适合我们真正喜欢的 Scala 特性,并且我们会被一些次优的东西困住.

i.e. If we had reified generics in the JVM, most likely those reified generics wouldn't be suitable for the features we really like about Scala, and we'd be stuck with something suboptimal.

这篇关于对于 Scala 来说,类型擦除有什么优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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