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

后端 未结 5 1932
谎友^
谎友^ 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:35

    Once scalac is a compiler, it has the potential of being able to embellish the generated code with whatever data structures are needed to implement reified generics.

    What I mean is that scalac would have the ability to see...

    // definition
    class Klass[T] {
      value : T
    }
    
    //calls
    floats  = Klass[float]
    doubles = Klass[double]
    

    ... and "expand" to something like this:

    // definition
    class Klass_float {
      value : float
    }
    class Klass_double {
      value : double
    }
    
    // calls
    floats  = Klass_float
    doubles = Klass_double
    

    Edit

    The point is: the compiler has the ability to create all necessary data structures which demonstrate to be necessary to provide additional type information at runtime. Once this type information is available, the Scala runtime would take advantage of it and could perform all type-aware operations we can imagine. It does not matter whether the JVM provides bytecode for reified generics or not. The work is not done by the JVM, but by the Scala library.

    If you have already written a symbolic debugger (I did!), you know that you can basically 'dump' all information the compiler has at compile-time into the generated binary, adopting whatever data organization demonstrates to be more convenient for further processing. This is exactly the same idea: 'dump' all type information the Scala compiler has.

    In a nutshell, I don't see why it could not be possible, does not matter whether the JVM provides native operations for reified generics or not. The JVM bytecode has nothing to do with reified generics. This sort of thing is a matter of language specification, compiler features and runtime library support.

    Another edit

    IBM X10 demonstrates the ability I'm talking of: it compiles X10 code onto Java code, leveraging reified generics onto Java platforms. As I mentioned before: it can be done (and IBM X10 did!) but this kind of feature involves language specification, compiler support (or compiler plugins) and enough support in runtime libraries. More info at: http://x10.sourceforge.net/documentation/papers/X10Workshop2012/slides/Takeuchi.pdf

提交回复
热议问题