调戏Scala解释器

第一感觉很像Matlab,命令行定义变量,查看运算值。Scala集合了很多的软件思想,不仅仅是OO和FP,还有分布式编程模式,超越的静态类型,一切皆同等地位的对象。

C:\Documents and Settings\Administrator>scala
Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.6.0_10).
Type in expressions to have them evaluated.
Type :help for more information.
//显示了当前使用的JVM版本

scala> 1+2
res0: Int = 3
//推断类型,分配临时对象,后面还可以使用,啥时候释放呢?

scala> res0 * 3
res1: Int = 9

scala> println ("hello world")
hello world
//内部函数调用

scala> val msg = "Hello, world!"
msg: java.lang.String = Hello, world!
//定义类似于final的字符串,变量是可以修改的,推断类型

scala> val msg2: java.lang.String = "Hell0 again, world!"
msg2: java.lang.String = Hell0 again, world!
//显式地指明类型

scala> val msg3: String = "Hello yet again, world!"
msg3: String = Hello yet again, world!
//Scala默认引入了String,需要指定包,跟Java一样

scala> println(msg)
Hello, world!
//带参数变量的函数调用

scala> msg = "Goodbye cruel world!"
<console>:8: error: reassignment to val
       msg = "Goodbye cruel world!"
           ^
//val定义的是值(value),不是说不能作为左值么

scala> var greeting = "Hello, world!"
greeting: java.lang.String = Hello, world!
//var定义的变量是可变的

scala> greeting = "Leave me alone, world!"
greeting: java.lang.String = Leave me alone, world!
//修改变量值

scala> var multiline =
     | "This is the next line."
multiline: java.lang.String = This is the next line.
//敲回车,Scala知道你还没有输完

scala> val oops =
     |
     |
You typed two blank lines.  Starting a new command.

scala>
//敲两次退出当前输入

scala> def max(x: Int, y: Int): Int = {
     | if(x > y) x
     | else y
     | }
max: (x: Int, y: Int)Int
//定义一个函数

scala> def max2(x: Int, y: Int) = if (x > y) x else y
max2: (x: Int, y: Int)Int
//相同函数,但是少打不少字,文本函数

scala> max(3, 5)
res4: Int = 5
//自定义函数调用

scala> def greet() = println("Hello, world!")
greet: ()Unit
//无参无返回值函数

scala> greet
Hello, world!
//自定义函数调用

scala> :quit
//睡觉

你可能感兴趣的:(调戏Scala解释器)