文法
- var 型(variable) と val 型(value)
- 前者は使ったら負け
- 後者は再代入できない
- 最初は var 型で,後々 val 型に移行するのがいいんじゃない?
scala> var n: Int = 1
n: scala.Int = 1
scala> n = 3
n = 3
scala> val n: Int = 1
n: scala.Int = 1
scala> n = 3
<console>:5 error: assignment to non-variable
n = 3
^
scala> val str: String = "hoge"
str: java.lang.String = hoge
scala> var sym: Symbol = 'piyo
sym: scala.Symbol = 'piyo
scala> val l: List[Int] = List(1, 2, 3)
l: scala.List[scala.Int] = List(1,2,3)
scala> val l: List[Any] = List(1, "hoge", 4.2)
l: scala.List[scala.Any] = List(1,hoge,4.2)
scala> Nil
line9: scala.Nil.type = List()
scala> List()
line10: scala.List[scala.Nothing] = List()
scala> null
line14: scala.Null = null
scala> 1::2::3::Nil
line15: scala.List[scala.Int] = List(1,2,3)
scala> Console println "Hello"
Hello
line18: scala.Unit = ()
scala> Console.println("Hello")
Hello
line19: scala.Unit = ()
def function(v1:Type, ....):Type = {
processes....
}
scala> def max(x: Int, y: Int): Int = { if ( x < y ) y else x }
max: (scala.Int,scala.Int)scala.Int
scala> max(1, 3)
line21: scala.Int = 3
scala> max(4, 3)
line22: scala.Int = 4
scala> val d = List(0.1, 4, "hoge")
d: scala.List[scala.Any] = List(0.1,4,hoge)
scala> def hello = "Hello, world!"
hello: => java.lang.String
scala> List(3, 34, 5) == List.apply(3, 34, 5)
line1: scala.Boolean = true
if (str.size < 3) {
"It's short."
} else if (str.size < 6) {
"Not so long."
} else "It's long."
for (ブロック引数 <- コレクション; ...) 処理内容
for (i <- 1 to 10 if i % 2 == 0) print(i + " ")
2 4 6 8 10
for (i <- 1 to 2; j <- 1 to 3) print "[" + i + "][" + j + "]"
List(1,2,3).map(n => n + 1)
scala> val sq = (n: Int) => Math.pow(n, 2).toInt
sq: (Int) => Int = <function>
scala> sq(6)
res0: Int = 36
scala> val half = (n: Int) => n / 2
half: (Int) => Int = <function>
scala> half(14)
res1: Int = 7
scala> def quartize(n: Int) = n / 4
quartize: (Int)Int
scala> val quarter = quartize _
quarter: (Int) => Int = <function>
scala> quarter(20)
res2: Int = 5
var str = "world"
str match {
case "world" => println("Hello")
case _ => ()
}
List(1, 2, 3) match {
case List(a, b, c) => a + b + c
case _ => 0
}
def sumRec(n: Int): Int = n match {
case 1 => 1
case _ => n + sumRec(n - 1)
}
scala> def multi(n: Int)(m: Int) = m * n
multi: (Int)(Int)Int
scala> multi(6)(9)
res5: Int = 54
scala> def multiTwo = multi(2)_
multiTwo: (Int) => Int
scala> multiTwo(5)
res6: Int = 10
scala> def multiTwo(n: Int) = multi(n)(2)
multiTwo: (Int)Int
scala> multiTwo(5)
res7: Int = 10
class SchrodingerCat {
lazy val status = {
println("Here open a box..."); "alive!"
}
}
val cat = new SchrodingerCat
cat.status
scala> class Cat
defined class Cat
scala> class Man { def greet = "Hello!" }
defined class Man
scala> implicit def cat2man(c: Cat): Man = new Man
cat2man: (Cat)Man
scala> val cat = new Cat
cat: Cat = Cat@bc5245
scala> cat.greet
res8: java.lang.String = Hello!
- Structual Subtyping
- Scala でダックタイピングっぽいことをやるための仕組み
scala> class Cat
defined class Cat
scala> class Duck {
| def swim = ()
| def quack = "Quaaa!"
| }
defined class Duck
scala> def duckTest[T](x: T { def swim; def quack: String }) = "You're duck!"
duckTest: [T](T{def swim: Unit; def quack: String})java.lang.String
scala> duckTest(new Duck)
res10: java.lang.String = You're duck!
scala> duckTest(new Cat)
<console>:11: error: type mismatch;
found : Cat
required: ?{def swim: Unit; def quack: String}
duckTest(new Cat)
^