Scala 的哪些功能无法转换为 Java?

发布于 2024-10-05 04:13:06 字数 244 浏览 0 评论 0原文

Scala 编译器直接编译为 Java 字节代码(或 .NET CIL)。 Scala 的一些功能可以直接用 Java 重新完成(例如简单的推导式、类、翻译匿名/内部函数等)。有哪些功能无法以这种方式翻译?

这大概主要是出于学术兴趣。也许更有用的是,您使用的 Scala 的哪些关键功能或习惯用法无法轻松地用 Java 表示?

还有其他办法吗?在 Java 中可以直接完成但在 Scala 中没有直接等效的事情? Java 中无法翻译的习语?

The Scala compiler compiles direct to Java byte code (or .NET CIL). Some of the features of Scala could be re-done in Java straightforwardly (e.g. simple for comprehensions, classes, translating anonymous/inner functionc etc). What are the features that cannot be translated that way?

That is presumably mostly of academic interest. More usefully, perhaps, what are the key features or idioms of Scala that YOU use that cannot be easily represented in Java?

Are there any the other way about? Things that can be done straightforwardly in Java that have no straightforward equivalent in Scala? Idioms in Java that don't translate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我不咬妳我踢妳 2024-10-12 04:13:06

在我看来,这个问题没有抓住要点,要求我们通过查看生成的字节码来比较 JVM 语言。

Scala 编译为与 Java 等效的字节码。也就是说,字节码可能是由 Java 编写的代码生成的。事实上,您甚至可以让 scalac 输出看起来很像 Java 的中间形式。

所有特性,如特征(通过静态转发器)、非本地返回(通过异常)、惰性值(通过引用)等都可以通过Java程序来表达,尽管可能是以一种最丑陋的方式!

但是 scala scala 而不是 Java 的原因是 scalac 可以在生成字节码之前为您做的事情。作为一种静态类型语言,scalac 的优点是能够在编译时检查程序的正确性,包括类型正确性(根据其类型系统) 。

因此,Java 和 scala 之间的主要区别(当然 Java 也是静态类型的)是 scala 的类型系统,它能够表达 java 语言的类型的编程关系系统不能。例如:

class Foo[M[_], A](m : M[A])
trait Bar[+A]

这些概念,即M是一个本身具有类型参数的类型参数,或者Bar是协变的,只是不存在于爪哇地区。

This question, in my opinion, misses the point about by asking us to compare JVM languages by looking at their generated bytecode.

Scala compiles to Java-equivalent bytecode. That is, the bytecode could have been generated by code written in Java. Indeed you can even get scalac to output an intermediate form which looks a lot like Java.

All features like traits (via static forwarders), non-local returns (via exceptions), lazy values (via references) etc are all expressible by a Java program, although possibly in a most-ugly manner!

But what makes scala scala and not Java is what scalac can do for you, before the bytecode is generated. What scalac has going for it, as a statically typed language, is the ability to check a program for correctness, including type correctness (according to its type system) at compile time.

The major difference then between Java and scala (as of course Java is also statically typed), therefore, is scala's type system, which is capable of expressing programmatic relations which java-the-language's type system cannot.For example:

class Foo[M[_], A](m : M[A])
trait Bar[+A]

These concept, that M is a type parameter which itself has type parameters or that Bar is covariant, just do not exist in Java-land.

败给现实 2024-10-12 04:13:06

特质是一种没有等价物的东西。特征是其中包含代码的接口。您可以将代码复制到混合有特征的所有类,但这不是同一件事。

我也相信 scala 类型系统更完整。虽然它最终会映射到 JVM 类型(实际上会被擦除)。您可以在 Scala 类型系统中表达一些在 Java 中可能无法实现的内容(例如差异)。

Traits are one thing that does not have an equivalent. Traits are Interfaces with code in them. You can copy the code to all classes that have a trait mixed in, but that is not the same thing.

Also I believe scala type system is more complete. While it will eventually map to the JVM types (actually suffer erasure). You can express some things in the Scala type system that may not be possible in Java (like variances).

∝单色的世界 2024-10-12 04:13:06

我认为,在某些特征中动态混合是没有等价的。在 Scala 中,您可以在创建新对象时添加一些特征,这些特征会混合在一起。

例如,我们创建一只又饿又渴的狗和一只只是饿的狗。

val hungryThirstyDog = new Dog with Hungry with Thirsty
val onlyHungryDog = new Dog with Hungry

我不知道在 Java 中执行此操作的等效方法。在Java中,继承是静态定义的。

I think, there is no equivalent for dynamically mix in some Traits. In Scala you can add at the time you're creating new objects some Traits, which are mixed in.

For example, we create one dog which is hungry and thirsty and one dog which is just hungry.

val hungryThirstyDog = new Dog with Hungry with Thirsty
val onlyHungryDog = new Dog with Hungry

I don't know an equivalent way to do this in Java. In Java, the inheritance is statically defined.

木格 2024-10-12 04:13:06

隐式转换在 Java 中没有直接的等价物。

Implicit conversions don't have a straightforward equivalent in Java.

江湖正好 2024-10-12 04:13:06

我发现 scala 的一个很好用的功能是通过 Manifests 进行类型具体化。由于 JVM 会从泛型中去除所有类型信息,因此 scala 允许您将这些信息保存在变量中。这是 Java 反射 AFAIK 无法处理的事情,因为字节码中没有类型的参数。

我需要它们的情况是对 List 类型进行模式匹配。也就是说,我有一个 VertexBuffer 对象,它将数据存储在 GPU 上,可以从浮点数或整数列表构造。清单代码大致如下:

class VertexBuffer[T](data:List[T])(implicit m:Manifest[T]) {
  m.toString.match {
    case "float" => ...
    case "int" => ...
  }
}

此链接包含更多信息的博客文章链接。

还有很多包含更多信息的 SO 页面,例如 这个

One feature of scala that I have found a good use for is type reification through Manifests. Since the JVM strips out all type information from generics, scala allows you to conserve this information in variables. This is something that Java reflection AFAIK can't handle, since there are no arguments to types in the bytecode.

The case I needed them was to pattern match on a type of List. This is, I had a VertexBuffer object which stored data on the GPU, that could be constructed from a List of floats or integers. The Manifest code looked approximately like this:

class VertexBuffer[T](data:List[T])(implicit m:Manifest[T]) {
  m.toString.match {
    case "float" => ...
    case "int" => ...
  }
}

This link links to a blog post with more information.

There are plenty of SO pages with more information too, like this one.

回梦 2024-10-12 04:13:06

三个词:高等类型。

Three words: higher kinded types.

雨落星ぅ辰 2024-10-12 04:13:06

您的主题不清楚您是指 Java JVM 还是 Java 语言。鉴于 Scala 运行在 JVM 上,q 没有任何意义,因为我们都知道 Scala 运行在 JVM 上。

Your topic is not clear wehther you mean Java the JVM or Java the language. Given that Scala runs on the JVM, the q makes no sense, as we all know Scala runs on the JVM.

脸赞 2024-10-12 04:13:06

Scala 对 XML 有“本机”支持。您可以直接在 Scala 代码中构建 XML、查找元素、匹配。

示例: http://programming-scala.labs.oreilly.com/ch10.html

Scala has a "native" support for XML. You can build the XML, find elements, match directly in the Scala code.

Examples: http://programming-scala.labs.oreilly.com/ch10.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文