Scala 类型系统的优点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3112725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:15:22  来源:igfitidea点击:

Advantages of Scala's type system

scalatypes

提问by Binil Thomas

I am exploring the Scala language. One claim I often hear is that Scala has a strongertype system than Java. By this I think what people mean is that:

我正在探索 Scala 语言。我经常听到的一种说法是 Scala 的类型系统比 Java。我认为人们的意思是:

  • scalacrejects certain buggy programs which javacwill compile happily, only to cause a runtime error.
  • Certain invariants can be encoded in a Scala program such that the compiler won't let the programmer write code that violates the condition.
  • scalac拒绝某些javac可以愉快编译的错误程序,只会导致运行时错误。
  • 某些不变量可以在 Scala 程序中编码,这样编译器就不会让程序员编写违反条件的代码。

Am I right in thinking so?

我这样想对吗?

回答by VonC

The main advantage of the Scala Type system is not so much being strongerbut rather being far richer(see "The Scala Type System").
(Java can define some of them, and implement others, but Scala has them built-in).
See also The Myth Makers 1: Scala's "Type Types", commenting Steve Yegge's blog post, where he "disses" Scala as "Frankenstein's Monster" because "there are type types, and type type types".

Scala 类型系统的主要优势不是更强大,而是更丰富(参见“ Scala 类型系统”)。
(Java 可以定义其中一些,并实现其他,但 Scala 内置了它们)。
另请参阅The Myth Makers 1: Scala 的“Type Types”,评论Steve Yegge 的博客文章,在那里他将 Scala 称为“弗兰肯斯坦的怪物”,因为“存在类型类型和类型类型”。

回答by Daniel C. Sobral

The main safety problem with Java relates to variance. Basically, a programmer can use incorrect variance declarations that may result in exceptions being thrown at run-time in Java, while Scala will not allow it.

Java 的主要安全问题与方差有关。基本上,程序员可以使用不正确的方差声明,这可能会导致 Java 在运行时抛出异常,而 Scala 不允许这样做。

In fact, the very fact that Java's Arrayis co-variant is already a problem, since it allows incorrect code to be generated. For instance, as exemplified by sepp2k:

事实上,JavaArray是协变的这一事实本身就已经是一个问题,因为它允许生成不正确的代码。例如,以sepp2k 为例

String[] strings = {"foo"};
Object[] objects = strings;
objects[0] = new Object();

Then, of course, there are raw types in Java, which allows all sort of things.

然后,当然,Java 中有原始类型,它允许各种各样的事情。

Also, though Scala has it as well, there's casting. Java API is rich in type casts, and there's no idiom like Scala's case x: X => // x is now safely cast. Sure, one case use instanceofto accomplish that, but there's no incentive to do it. In fact, Scala's asInstanceOfis intentionally verbose.

此外,虽然 Scala 也有它,但有强制转换。Java API 具有丰富的类型转换,并且没有像 Scala 的case x: X => // x is now safely cast. 当然,有一个案例用于instanceof实现这一目标,但没有动力去做。事实上,ScalaasInstanceOf是故意冗长的。

These are the things that make Scala's type system stronger. It is also much richer, as VonCshows.

这些都是使 Scala 的类型系统更强大的东西。正如VonC所示,它也更加丰富。