Search code examples
.netscalagenericslanguage-designtype-erasure

Reified generics in Scala on .NET/CLR


Scala (at least on the JVM) uses type erasure for Java compatibility. This feature is widely held to suck. Fixing this would be difficult on the JVM.

In contrast to the JVM situation, .NET supports reified generics. Does Scala's .NET implementation use them? If not, could it, or else what issues would using reification cause?


Solution

  • It's work in progress, carefully not to break Scala semantics between JVM and .NET.

    I asked this question back in 2011 on the scala-tools mailinglist and the answer is given by Miguel Garcia in which he outlines the big picture:

    Some quotes:

    (1) What the Scala.Net preview currently does. As you have noticed, the erasure phase also runs as part of the pipeline. This is a "feature" of the preview version, a "feature" that had to be included because support for CLR Generics wasn't there yet (more on this below). There is however one big advantage to running JVM-style erasure in Scala.Net: all the Scala programs out there that rely on the Scala library can already be compiled on .Net, instead of waiting for CLR Generics to be ready. Those programs that rely on the Java JDK can also be compiled, subject to IKVM support of the JDK APIs in question [1].

    (2) Support for CLR Generics in Scala.Net. The main motivation to support it is gaining interoperability with existing assemblies. In gaining that interoperability, care will be taken not to break away from Scala semantics. In other words, any valid Scala program is going to run and produce the same results on JVM and .NET. Which brings us to the work in progress [2]. The initial prototype handles only the C# subset of Scala. So now I'm addressing the rest. It's more work than initially anticipated but it's important to cover the whole language.

    A few more comments regarding interop with .NET assemblies, in particular native issues. Yes, CLR assemblies can express using "native int" (different sizes on different CPUs), P/Invoke of C-functions exported by a .dll and such. Scala.Net does not aim to do that low-level trickery. The assembly interoperability of interest is at the level of "Common Language Specification", i.e. what one normally obtains from any C#, VB.NET, etc. compiler ("normally" i.e. unless using "[DllImport]" attributes and related C++-isms).

    Quoting from the CLI spec:

    --- start quote --- The Common Language Specification (CLS) -- The CLS is an agreement between language designers and framework (that is, class library) designers. It specifies a subset of the CTS (Common Type System) and a set of usage conventions. Languages provide their users the greatest ability to access frameworks by implementing at least those parts of the CTS that are part of the CLS. Similarly, frameworks will be most widely used if their publicly exported aspects (e.g., classes, interfaces, methods, and fields) use only types that are part of the CLS and that adhere to the CLS conventions. --- end quote ---

    see for the whole thread:

    https://groups.google.com/forum/?fromgroups#!topic/scala-tools/JDjstK1_uvM