Is it possible for Scala to have reified generics without changing the JVM?

后端 未结 5 1901
谎友^
谎友^ 2021-01-03 19:29

I\'ve recently started learning Scala and was disappointed (but not surprised) that their generics are also implemented via type erasure.

My question is, is it possi

5条回答
  •  心在旅途
    2021-01-03 19:56

    To complement oxbow_lakes answer: It is no possible and it seems it will never happen (at least soon).

    The (refutable) reasons JVM will not support reified generics seems to be:

    • Lower performance.
    • It breaks backward compatibility. It can be solved duplicating and fixing a lot of libraries.
    • It can be implemented using manifests: The "solution" and the biggest impediment.

    References:

    • Odersky comment in 2010: "I prefer a simpler VM architecture with type erasure"

    • In scala-internals list (Feb 2013) Grzegorz Kossakowski said:

    You can easily benchmark it and see that performance impact is very noticeable. Especially memory consumption increases a lot.

    I believe the way to go is to have optional reification the way we start to do in Scala with Manifests/TypeTags.

    If you can and combine it with runtime specialization you can aim for high performance and generic code. However, that's probably goal for Scala 2.12 or 2.13.

提交回复
热议问题