Back to Computer Languages

See Also Java

Scala

Scala(/ˈskɑːlɑː/)是一门多范式的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。

1. History

Scala设计开始于2001年,作者Martin Odersky开发了Java的范型和Sun的Java编译器;

在2004年早期Scala正式发布,第二版(v2.0)在2006年3月。

2. Platforms and license

Scala运行于Java平台(Java虚拟机),并兼容现有的Java程序。它也能运行于CLDC配置的Java ME中。目前还有另一.NET平台的实现,不过该版本更新有些滞后。

Scala的编译模型(独立编译,动态类加载)与Java和C#一样,所以Scala代码可以调用Java类库(对于.NET实现则可调用.NET类库)。

Scala包包括编译器和类库,以BSD许可证发布。

2.1. Other Compilers and Targets

Scala.js is a Scala compiler that compiles to JavaScript

Lua 可以调用 lua 或者 C 实现的函数。Lua 所有标准库都是用 C 实现的。标准库包 括 string 库、table 库、I/O 库、OS 库、算术库、debug 库。

3. Examples

List("Hello", "World").mkString("", ", ", "!")

3.1. "Hello World" example

 object HelloWorld extends App {
   println("Hello, World!")
 }

3.2. Basic example


3.3. Example with classes

// Scala
class Point(
    val x: Double, val y: Double,
    addToGrid: Boolean = false
) {
  import Point._

  if (addToGrid)
    grid.add(this)

  def this() = this(0.0, 0.0)

  def distanceToPoint(other: Point) =
    distanceBetweenPoints(x, y, other.x, other.y)
}

object Point {
  private val grid = new Grid()

  def distanceBetweenPoints(x1: Double, y1: Double,
      x2: Double, y2: Double) = {
    math.hypot(x1 - x2, y1 - y2)
  }
}

4. Getting Started

https://www.scala-lang.org/documentation/getting-started.html

4.1. Installation

Getting Started with Scala and sbt on the Command Line

➜  w2016 java -version   
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
➜  w2016 brew install sbt
...
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to SBT:
   SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"

4.2. Create the project

Getting Started with sbt sbt is a build tool for Scala, Java, and more. It requires Java 1.8 or later.

➜  scala sbt new scala/hello-world.g8
...
name [Hello World template]: hello-world

Template applied in ./hello-world
➜  hello-world sbt        
[info] Loading project definition from /private/tmp/scala/hello-world/project
[info] Loading settings from build.sbt ...
[info] Set current project to hello-world (in build file:/private/tmp/scala/hello-world/)
[info] sbt server started at 127.0.0.1:5921       
sbt:hello-world> ~run
[info] Running Main 
Hello, World!
[success] Total time: 1 s, completed Sep 19, 2017 3:51:44 PM
1. Waiting for source changes... (press enter to interrupt)
[info] Compiling 1 Scala source to /private/tmp/scala/hello-world/target/scala-2.12/classes ...
[info] Done compiling.
[info] Running Main 
Hello, New York!
[success] Total time: 1 s, completed Sep 19, 2017 3:52:25 PM
2. Waiting for source changes... (press enter to interrupt)

4.3. Scala Test Tutorial

Testing Scala with sbt and ScalaTest on the Command Line

➜  scala sbt new scala/scala-seed.g8
...
name [Scala Seed Project]: ScalaTestTutorial
...
➜  scalatesttutorial sbt test
[info] Loading project definition from /private/tmp/scala/scalatesttutorial/project
[info] Set current project to Hello (in build file:/private/tmp/scala/scalatesttutorial/)
[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] Run completed in 311 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed Sep 19, 2017 4:17:37 PM

5. Scala School!

http://twitter.github.io/scala_school/

5.1. Basics

http://twitter.github.io/scala_school/zh_cn/basics.html

6. Reference

MainWiki: Scala (last edited 2017-09-18 22:03:51 by twotwo)