.net - .NET/CLR上Scala中的改进泛型

标签 .net scala generics language-design type-erasure

Scala(至少在JVM上)使用type erasure来实现Java兼容性。此featurewidely held to suckFixing this would be difficult on the JVM

In contrast to the JVM situation, .NET supports reified generics. Scala's .NET implementation是否使用它们?如果不是,那会否,否则使用修正会导致什么问题?

最佳答案

它正在进行中,请注意不要破坏JVM和.NET之间的Scala语义。

我在2011年在scala-tools邮件列表上问了这个问题,答案是Miguel Garcia给出的,他概述了总体情况:

一些引号:

(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 ---



看到整个线程:

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

关于.net - .NET/CLR上Scala中的改进泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607170/

相关文章:

java - Scala:使用 circe.io 解码 Any[] 数组

c# - 通过反射调用带有参数的泛型方法时,无法将 System.Int32 类型的对象转换为 System.Object[]

java - 将泛型 T 转换为 Object[]

c# - 类库中的 .net Reference Web

c# - 检查附加对象是否有任何基于导航属性的相关数据库

scala - 你如何使用标准库编写 foldWhile?

scala - 在 EMR 环境中将 JAR 提交到 Spark 时出现 FileNotFoundException (stderr & stdout)

c# - 如何从泛型列表中提取派生类型?

c# - 同一类的单元测试(有条件编译)?

c# - 来自 Gridview 中 UserControl 的事件未被触发