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
Advantages of Scala's type system
提问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 whichjavacwill 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 称为“弗兰肯斯坦的怪物”,因为“存在类型类型和类型类型”。
- Value type classes (useful for reasonably small data structures that have value semantics) used instead of primitives types (Int, Doubles, ...), with implicit conversion to "Rich" classes for additional methods.
- Nonnullable type
- Monad types
- Trait types (and the mixin compositionthat comes with it)
- Singleton object types (just define an 'object' and you have one),
- Compound types(intersections of object types, to express that the type of an object is a subtype of several other types),
- Functional types(
(type1, …)=>returnTypesyntax), - Case classes(regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching),
- Path-dependent types(Languages that let you nest types provide ways to refer to those type paths),
- Anonymous types(for defining anonymous functions),
- Self types(can be used for instance in Trait),
- Type aliases, along with:
- package object(introduced in 2.8)
- Generic types(like Java), with a type parameter annotation mechanismto control the subtyping behavior of generic types,
- Covariant generic types: The annotation
+Tdeclares typeTto be used only in covariant positions.Stack[T]is a subtype ofStack[S]ifTis a subtype ofS. - Contravariant generic types:
-Twould declareTto be used only in contravariant positions.
- Covariant generic types: The annotation
- Bounded generic types (even though Java supports some part of it),
- Higher kinded types, which allow one to express more advanced type relationships than is possible with Java Generics,
- Abstract types(the alternative to generic type),
- Existential types(used in Scalalike the Java wildcard type),
- Implicit types(see "The awesomeness of Scala is implicit",
- View bounded types, and
- Structural types, for specifing a type by specifying characteristics of the desired type (duck typing).
- 使用值类型类(对于具有值语义的相当小的数据结构有用)而不是原始类型(Int、双精度数、...),隐式转换为“Rich”类以用于其他方法。
- 不可为空类型
- 单子类型
- Trait 类型(以及它附带的mixin 组合)
- 单例对象类型(只需定义一个“对象”,您就拥有了一个),
- 复合类型(对象类型的交集,表示一个对象的类型是其他几种类型的子类型),
- 函数类型(
(type1, …)=>returnType语法), - 案例类(导出其构造函数参数并通过模式匹配提供递归分解机制的常规类),
- 依赖于路径的类型(让您嵌套类型的语言提供了引用这些类型路径的方法),
- 匿名类型(用于定义匿名函数),
- Self 类型(例如可以在 Trait 中使用),
- 键入 aliases,以及:
- 包对象(在 2.8 中引入)
- 泛型类型(如 Java),具有类型参数注释机制来控制泛型类型的子类型行为,
- 协变泛型类型:注释
+T声明类型T仅用于协变位置。Stack[T]是Stack[S]ifT的子类型 是 的子类型S。 - 逆变泛型类型:
-T将声明T仅用于逆变位置。
- 协变泛型类型:注释
- 有界泛型类型(即使Java 支持它的某些部分),
- 更高级的类型,允许表达比 Java 泛型更高级的类型关系,
- 抽象类型(泛型类型的替代),
- 存在类型(在 Scala 中像 Java 通配符类型一样使用),
- 隐式类型(参见“ 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所示,它也更加丰富。

