Scala8

来源:互联网 发布:宁波数据分析招聘 编辑:程序博客网 时间:2024/06/06 18:22

Stateful Objects

  • 这一章主要介绍 stateful object,即 mutable object

  • 对于 purely functional object,每次访问都可以得到同样的结果。然而对于 stateful object, the result of a method call or field access may depend on what operations were previously performed on object.

  • 一般地,如果使用了 var,那么就可能是 statefull object,但是这样的结论并不是完全成立的。For instance, a class might be stateful without defining or inheriting any vars because it forwards method call to other objects that have mutable state. 那么反过来也可能:A class might contain vars and still be purely functional.

  • In Scala, every var that is a non-private member of some object implicitly defines a getter and a setter method with it. The getter of a var x is just name “x”, while its setter is named “x_=”

  • The getter and setter, get the same visibility as the original var

  • 有时可以不定义一个 var,而是直接定义一个 getter 以及一个 setter方法。By defining these access methods directly you can interpret the operations of variable access and variable assignment as you like.

  • 事实上, Scala’s convention of always interpreting a variable as a pair of setter and getter methods gives you in effect the same capabilities as C# properties without requiring special syntax(不过,没怎么学习过C#,对这句话怎什么感觉)

  • 另外,自己跳过了 Case study: Discrete event simulation,有时间的话再回来看看

Type Parameterization

  • 这一章主要讲Scala的 type paramterization 以及 information hiding. Type parameterization allows you to write generic classes and traits. Variance defines inheritance relationships of parameterized types, such as whether a Set[String], for example, is a subtype of Set[AnyRef].

  • 第一节先定义一个 functional queues,这里没什么好讲的,主要是为了引出 techniques to hide internal representation details of the structure.

  • Information hiding的方式有如下几种:

    a. Private constructors and factory methods

    b. hide the class itself and only export a trait that reveals the public interface of the class

  • Classes and traits that take type parameters are “generic”, but the types they generate are “parameterized”, not generic.

  • subtyping 和 type parameters是属于两个不同的概念。对于一个Class C,以及两个类型参数S、T,如果S是T的子类,那么Class[S] 和 Class[T]的关系呢?这里有三种关系:

    a. 如果说 Class[S] 是 Class[T] 的子类,那么就说 这个类是 covariant in its type parameter T.

    b. 如果说 Class[S] 是 Class[T] 的超类,那么就说这个类是 contravariant in its type parameter T.

    c. 如果说 Class[S] 与 Class[T] 没有关系,那么就说这个类是 invariant in its type parameter T.

    d. Whether a type parameter is covariant, contravariant, or nonvariant is called the parameter’s variance

  • Scala tries to be purer thant Java in not treating arrays as covariant. Of course, Scala will also let you cast an array of Ts to an array of any supertype of T: using isInstanceOf and asInstanceOf

  • variance 的使用还是挺让人confusing,自己总结了一个规则:如果一个位置定义为 negative postions, 那么不论怎么 flipped,最后的结果只能为 -,而不能是其他。 positive positions 同理。

  • 如果使用 variance不当,有时候就会出现这样的信息:

error: covariant type T occurs in contravariant position in type T of value X

那么这时,可以使用 lower bound 或者 upper bound来解决。一般使用 covariant就需要 lower bound,

  • 一般地,有这样的规定: contravariant in the function argument type S and covariant in result type T. This satisfies the Liskov substitution principle, because arguments are something that’s required, whereas results are something that’s provided.

  • In order to construct a case where variance would lead to type errors, you need to have a reference to a containing object that has a statically weaker type than the type the object was defined with.

  • 不过,说实话,自己把这一章都看了三遍了还是没怎么看懂,看来还要找点类型系统的资料来看看,先立个flag

0 0
原创粉丝点击