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 ?
  • 如果Scala 2.10的materialized 结果比KotlinCeylon更有限.为什么?

推荐答案

你的论点有缺陷.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.

Kotlin相关问答推荐

在KMP中使用koin将来自Android的上下文注入到SQLDelight Driver中

try 一次性插入多条记录时,JOOQ连接为空错误

为什么Kotlin有次构造函数和init块?

将 SharedPreferences 中的值公开为流

从 HashMap 检索时的 NPE,即使 containsKey() 在多线程环境中返回 true

PRDownloader 即使在实现库后也无法工作.未知参考:Android Studio 中的 PRDownloader

这是什么 Kotlin 类型:(String..String?)

添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障

为什么 IntelliJ Idea 无法识别我的 Spek 测试?

为什么 Kotlin 需要函数引用语法?

DatabaseManager_Impl 不是抽象的,不会覆盖 RoomDatabase 中的抽象方法 clearAllTables()

Android Kotlin StringRes 数量String

如何将 Kotlin 日期中的字符串或时间戳格式化为指定的首选格式?

如何将map函数应用于Kotlin中的数组并更改其值?

kotlin 委托有什么用?

通过在 kotlin-gradle 中使用子项目Unresolved reference: implementation

kotlin中密封类和密封接口的区别是什么

Kotlin var lazy init

是否可以在不使用class的情况下将 Mockito 与 Kotlin 一起使用?

如何判断数据是否插入到房间数据库中