gpt4 book ai didi

scala - 对于 Scala,类型删除有什么好处吗?

转载 作者:行者123 更新时间:2023-12-03 15:38:50 26 4
gpt4 key购买 nike

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

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

最佳答案

Ola Bini's blog .众所周知,Java 具有使用站点协方差,通过在您认为方差合适的地方添加小问号来实现。 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.

...

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 特性,而且我们会遇到一些次优的问题。

关于scala - 对于 Scala,类型删除有什么好处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com