scala入门
mac上安装
brew install scala
配置vim对scala高亮,使用Plugin的方式
Plugin 'derekwyatt/vim-scala'
:PluginInstall
. ~/.vimrc
编写helloworld, 新建文件 hello.scala
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, World!")
}
}
运行:
scala hello.scala
编译
scalac hello.scala
scala HelloWorld
还可以给scala指定选项, -d 指定生成的class文件目录
scalac -d classes hello.scala
也可以指定类路径,比如刚刚的类生成在classes文件夹下,运行就这么做:
scala -cp classes HelloWorld
注意: scala的参数必须是top-level对象,如果继承了trait App,那么对象里的所有语句都会被执行,如下。
object HelloWorld extends App {
println("Hello, World!")
}
否则的话,就要像前面的例子一样定义main方法。
交互式运行
在shell中输入代码:
localhost:~ yuandingping$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.
scala> object HelloWorld {
| def main(args: Array[String]): Unit = {
| println("Hello, World!")
| }
| }
defined object HelloWorld
scala> HelloWorld.main(Array())
Hello, World!
scala> q
<console>:12: error: not found: value q
q
^
scala> :q
localhost:~ yuandingping$
:q 表示退出scala环境。
脚本运行
scala还可以运行在脚本中, 比如script.sh:
#!/usr/bin/env scala
object HelloWorld extends App {
println("Hello, World!")
}
运行:
./script.sh
注意: script.sh必须有可执行权限,且scala命令加入了类路径
貌似下面的执行方法是不对的:
sh script.sh
但是可以这样:
scala script.sh
以上
原文链接
scala一般使用sbt管理工程,执行编译构建等工作:
brew install sbt
这里有很多scala的学习资料:
Comments