Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 possible for Scala to have reified generics, or would the JVM need to be changed in some way? If the JVM does need to be changed, what exactly would need to be changed?

like image 665
cdmckay Avatar asked Aug 31 '09 15:08

cdmckay


4 Answers

No - it is not possible for Scala to run as Java-equivalent bytecode if that bytecode does not support reified generics.

When you ask "what is it that needs to be changed?", the answer is: the bytecode specification. Currently the bytecode does not allow for the parametrized type of a variable to be defined. It has been decided that as a modification to the bytecode to support reified generics would break backwards compatibility, that generics would have to be implemented via type erasure.

In order to get around this, Scala has used the power of its implicit mechanism to define a Manifest which can be imported in any scope to discover type information at runtime. Manifests are experimental and largely undocumented but they are coming as part of the library in 2.8. Here is another good resource on Scala reified generics / Manifests

like image 198
oxbow_lakes Avatar answered Oct 09 '22 02:10

oxbow_lakes


Just to complement oxbow_lakes, there's a question on Stack Overflow about how to get around type erasure in Scala.

like image 42
Daniel C. Sobral Avatar answered Oct 09 '22 03:10

Daniel C. Sobral


"implicit Manifest" is a Scala compiler trick and it does not make generics in Scala reified. The Scala compiler, when it sees a function with "implicit m: Manifest[A]" parameter and it knows the generic type of A at the call site, it will wrap the class of A and its generic type parameters into a Manifest and make it available inside the function. However, if it could not figure out the true type of A, then it has no way of creating a Manifest. In other words, Manifest has to be passed along the function calling chain if the inner function needs it.

scala> def typeName[A](a: A)(implicit m: reflect.Manifest[A]) = m.toString
typeName: [A](a: A)(implicit m: scala.reflect.Manifest[A])java.lang.String

scala> typeName(List(1))
res6: java.lang.String = scala.collection.immutable.List[int]

scala> def foo[A](a: A) = typeName(a)
<console>:5: error: could not find implicit value for parameter m:scala.reflect.Manifest[A].
       def foo[A](a: A) = typeName(a)
                                  ^

scala> def foo[A](a: A)(implicit m: reflect.Manifest[A]) = typeName(a)
foo: [A](a: A)(implicit m: scala.reflect.Manifest[A])java.lang.String

scala> foo(Set("hello"))
res8: java.lang.String = scala.collection.immutable.Set[java.lang.String]
like image 43
Walter Chang Avatar answered Oct 09 '22 01:10

Walter Chang


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.

like image 32
santiajo Avatar answered Oct 09 '22 01:10

santiajo