Scala 类型系统的优点 [英] Advantages of Scala's type system

查看:37
本文介绍了Scala 类型系统的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  • scalac 拒绝某些 javac 可以顺利编译的错误程序,只会导致运行时错误.
  • 某些不变量可以在 Scala 程序中进行编码,这样编译器就不会让程序员编写违反条件的代码.
  • scalac rejects certain buggy programs which javac will 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.

我这样想对吗?

推荐答案

Scala Type 系统的主要优势不是更强大,而是更丰富(参见Scala 类型系统").
(Java 可以定义其中的一些,并实现其他的,但 Scala 内置了它们).
另请参阅 神话制造者 1:Scala 的类型类型",评论 Steve Yegge 的博文,其中他贬低"Scala 为弗兰肯斯坦的怪物",因为有类型类型,还有类型类型".

The main advantage of the Scala Type system is not so much being stronger but 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".

  • 使用值类型类(对于具有值语义的相当小的数据结构很有用),而不是原始类型(Int、Doubles 等),并隐式转换为Rich"类以用于其他方法.
  • 不可为空类型
  • Monad 类型
  • 特征类型(以及 混合组合 附带的)
  • 单一对象类型(只需定义一个对象"即可),
  • 复合类型(对象类型的交集,表示一个object 是其他几种类型的子类型),
  • 函数类型((type1, …)=>returnType 语法),
  • 案例类(导出其构造函数参数并提供递归的常规类通过模式匹配的分解机制),
  • 路径依赖类型(允许嵌套类型的语言提供引用这些类型路径的方法),
  • 匿名类型(用于定义匿名函数),
  • 自我类型(可以是例如在 Trait 中使用),
  • 输入别名,以及:
  • 包对象(在 2.8 中引入)
  • 泛型类型(如 Java),带有 类型参数注解机制来控制泛型类型的子类型化行为,
    • 协变泛型类型:注释+T 声明类型T 仅用于协变位置.Stack[T]Stack[S] 的子类型,如果 TS 的子类型.
    • 逆变泛型类型:-T 将声明 T 仅用于逆变位置.
    • 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 composition that 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, …)=>returnType syntax),
    • 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 mechanism to control the subtyping behavior of generic types,
      • Covariant generic types: The annotation +T declares type T to be used only in covariant positions. Stack[T] is a subtype of Stack[S] if T is a subtype of S.
      • Contravariant generic types: -T would declare T to be used only in contravariant positions.

      这篇关于Scala 类型系统的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆