
这里列出在开发一个Scala工程中需要参照的资料。
http://www.scala-lang.org/
http://docs.scala-lang.org/index.html
http://docs.scala-lang.org/cheatsheets/
http://docs.scala-lang.org/style/
https://wiki.scala-lang.org/display/SYGN/Design+Patterns
http://www.scala-lang.org/files/archive/spec/2.11/
http://www.scala-lang.org/api/current/#package
https://wiki.scala-lang.org/display/SW/Tools+and+Libraries
Scala是由Martin Odersky设计的一种结合了函数式编程(不仅仅有这个)的强类型语言。 Scala的代码会被编译成Java bytecode,并在JVM上运行的。 可以认为Scala是Java的扩充,一个Java语言的超集。 Scala的设计思想之一,来自于对Java语言的一些缺点的批评。
个人也觉得Java语言发展的太慢,尤其是和C#、.NET相比。 Scala的横空出世,提供了一个积极性的灵感,解决了Java语言的发展问题,给Java社区带来强大的活力。
由于Scala结合了函数式编程和面向对象语言特征,从这个方面看,有两种编程风格可以选用。
如果你开发的项目主要用于处理数据,函数式编程风格更适用。 本文也主要针对函数式编程风格。
可以从scala的官方网站上下载。 许多人会选择Intellij IDEA或者Eclipse作为scala的编辑器。
这里有个使用Visual Studio Code作为编辑器的介绍: Scala on Visual Studio Code
请参考:函数式编程 : 一个程序猿进化的故事
开始学习Scala会看到trait这个奇怪的词,而且可以def object。这里解释一下: trait: 类似于Interface,不过可以实现声明的方法。 class: 就是class. object: 就是Module,一个静态类。
除了Java的语言特征外,Scala还提供了一下主要特征。 (这个章节比较深奥,可能不足,需要慢慢地更新)
上面已经说过了。
这个特征C#也有。建议大家尽量使用这个特点。也就是说
我把对implicit的认识分为这几个Levels。
如果,你在你的项目里发现有人使用implicit,可以理直气壮地批评,这是降低可读性的杰作。 implicit特性应该避免被使用。implicit很可能给读代码的人带来困惑,属于反直觉的代码。
比如:一个函数的参数的Long型,调用时输入一个Int型的数据,也不会出错, 其后面就是implicit conversions功劳。 implicit conversions逻辑发现类型Int/Long不匹配, 然后在一个implicit view(可以看成一个函数pool,包含了所有的implicit functions)中找一个输入为Int,输出为Long的函数, 最后通过这个函数完成类型的转换。 Scala自身提供了一些必要的implcite conversion functions.
使用起来很简单,直接把implicit关键字,加到trait/class/object/def/val之前就可以。 在Scala 2.10版后, implicit可以用在三个地方: 例如:
object ImplicitTest extends App {
  /** 
   *  To show how implicit parameters work, 
   *  we first define monoids for strings and integers. 
   *  The implicit keyword indicates that the corresponding object can be used 
   *  implicitly, within this scope, as a parameter of a function marked implicit. 
   */
  implicit object StringMonoid extends Monoid[String] {
    def add(x: String, y: String): String = x concat y
    def unit: String = ""
  }
  implicit object IntMonoid extends Monoid[Int] {
    def add(x: Int, y: Int): Int = x + y
    def unit: Int = 0
  }
  /** 
   *  This method takes a List[A] returns an A which represent the combined 
   *  value of applying the monoid operation successively across the whole list. 
   *  Making the parameter m implicit here means we only have to provide 
   *  the xs parameter at the call site, since if we have a List[A] 
   *  we know what type A actually is and therefore what type Monoid[A] is needed. 
   *  We can then implicitly find whichever val or object in the current scope 
   *  also has that type and use that without needing to specify it explicitly.
   */
  def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
    if (xs.isEmpty) m.unit
    else m.add(xs.head, sum(xs.tail))
  /** 
   *  Here we call sum twice, with only one parameter each time. 
   *  Since the second parameter of sum, m, is implicit its value is looked up
   *  in the current scope, based on the type of monoid required in each case, 
   *  meaning both expressions can be fully evaluated.
   */
  println(sum(List(1, 2, 3)))          // uses IntMonoid implicitly
  println(sum(List("a", "b", "c")))    // uses StringMonoid implicitly
}可以想到,implicit有个作用域。这个作用域,和当前的package,以及import的类, 还有Scala的默认有关。
请看Scala Collection简介
可变的变量(Mutable variables)(主要是可变的对象)会引起一些潜在的问题:
编程方面的建议是:
请请看Scala underscore的用途
  // There is no '=' before '{', so the function return Unit.
  def funUnit(x: Int) {
    x * x
  }
  // There is a '=' before '{', 
  // so the function returns the value of the last expression.
  def funReturnLastStatement(x: Int) = {
    x * x
    x + x
  }
  // Recommend to use explicit return type, and = forever.
  def funReturnLastStatementGood(x: Int) : Int = {
    x * x
    x + x
  }在Scala中, ##方法相当于Java中的hashCode方法。 ==方法相当于Java中的equals方法。 建议使用##和==,因为Scala针对value类型实现额外的功能。
请看不变(Invariant), 协变(Covarinat), 逆变(Contravariant) : 一个程序猿进化的故事
scalac -d test.jar D:\project\*