Scala教程

专注Scala

Null->null
Nothing
Nil->List[Nothing]
None->Option
Unit
很多人抱怨Scala相比于Java过于复杂了:大部分使用过Scala的程序员都没有能深刻理解它的类型系统和Scala的函数式编程。Scala的类型系统跟Java和C++很不一样,Scala想把面向对象纯粹化(学院派的作风?),不能有破坏面向对象的一切因素出现。nullNULLint、…这些都是不和谐的东西,应该割掉。Scala又给了太多了空间给程序员,你可以使用传统的命令式编程风格,也可以使用函数式风格,一个语言写出了不同的代码。这个跟汉语有很多方言,极为相似。太多的自由会浪费Scala的一片好心。一个思路是在封装完Java的功能后,尽量使用Scala推荐的函数式风格来写面向对象的程序。
进入Scala,你就进入了虚无缥缈的太虚境地。在何为有?何为无?的问题上?Scala的设计走得很远。Scala的有即Any,Scala的无是NullnullNilNothingNoneUnit。Scala的无太让人手足无措,今天就讨论Scala的无。
要想在正确的地方使用正确的无,就要先理解它们分别表示的含义。

Read on

In this chapter we will cover the core data and variable types in Scala. Let’s start with the definitions of the terms literal, value, variable, and type:

  • A literal (or literal data) is data that appears directly in the source code, like the number 5, the character A, and the text “Hello, World.
  • A value is an immutable, typed storage unit. A value can be assigned data when it is defined, but can never be reassigned.
  • A variable is a mutable, typed storage unit. A variable can be assigned data when it is defined and can also be reassigned data at any time.
  • A type is the kind of data you are working with, a definition or classification of data.

All data in Scala corresponds to a specific type, and all Scala types are defined as classes with methods that operate on the data.
Read on

The Scala programming language has a wonderfully continental(adj. 大陆的;大陆性的) ring to its name, as befits its origins at the École polytechnique fédérale de Lausanne (EPFL) in Lausanne,Switzerland. The Scala logo represents a circular stairway, which may lead you to believe its origin is the term La Scala, meaning a staircase or ladder in Italian, or that it derives from the famous Italian opera house Teatro alla Scala. In fact the name Scala is an abbreviation of the term SCAlable LAnguage, a fitting description of its intention. Professor Martin Odersky and his group at EPFL created the language in 2003 to provide a high-performance, concurrent-ready environment for functional programming and object-oriented programming on the Java Virtual Machine (JVM) platform.
Read on

self =>相当于给this起了一个别名为self

class A { 
    self =>  //this别名
    val x=2 
    def foo = self.x + this.x 
}

self不是关键字,可以用除了this外的任何名字命名(除关键字)。在A内部,可以用this指代当前对象,也可以用self指代,两者是等价的。
它的一个场景是用在有内部类中:
Read on