3. 紹介する Scala の機能
Structural subtyping
Case class + pattern matching
Lazy value
Implicit conversion(Views)
Singleton type
Generic type
Regular expression pattern
Existential Type
Abstract Type
Trait
4. Structural subtyping
静的型でダックタイピング ( 相当 ) ができ
るよ!
nominal VS. structural
2.6.0-RC1 で導入
def putsName(x :{ def name :String }) = println(x.name)
object Foo { def name = "Foo" }
object Bar { def name = "Bar" }
var x = List[{def name :String}](Foo, Bar)
for(p <- x) putsName(p)
Foo
Bar
5. Case class + pattern matching
関数型言語でおなじみ
case class で使用するデータ構造を定義
match 構文で pattern match を行う
abstract class Term
case class Num(x :int) extends Term
case class Plus(l :Term, r :Term) extends Term
...
Plus(Num(100), Num(200)) match {
case Num(a) => ...
case Plus(l, r) => println(l, r)
} (Num(100),Num(200))
7. Implicit conversion(Views)
ユーザが暗黙の型変換を定義可能
定義した変換はコンパイラの型チェックをパ
スする
標準でいくつかの暗黙の変換が定義
int から long, int から double など
implicit def i2s(i :int) = i.toString
println(321.substring(1)) 21
用法・用量をよく守ってお使いください
こんなことをしてはいけません
implicit def s2i(s :String) = Integer.parseInt(s)
var i :int = "hoge"
java.lang.NumberFormatException: For input string: "hoge"
8. Singleton type
特定のインスタンスを参照していることを
保証
「自分自身」 (this) を返す型などが作れる
カスケード呼び出しをするときなどに役立
つ
class Super {
def putsSuper() :this.type = { print("Super"); this }
}
class Sub extends Super {
def putsSub() :this.type = { println("Sub"); this }
}
...
new Sub().printSuper.printSub SuperSub
9. Generic type
Java generics と仕様は類似
実は GJ の人 =Scala の人
class Stack[T] {
def push(e :T) = ...
def pop = ...
...
}
...
val s = new Stack[String]
s push "Foo"
println(s pop) Foo
10. Regular expression pattern
現在は限定的なサポート
Sequence のパターンマッチで末尾に任意個
の要素が来る場合を扱える
var x = Array("Foo", "Bar", "Baz")
x match { case Array("Foo", _ )* => println("Foo") }
これを書いたらエラーメッセージ が表示され
た orz
x match {
case Array("Foo", _ , "Baz") => println("Foo")*
}
regular expressions not yet implemented
11. Existential type
generic な型同士にサブタイプを導入でき
る
Array[String] は Array[Any] のサブタイプ
ではない
var x:Array[T forSome {type T <: Any}] = Array("A", "B")
x = Array(1, 2) OK!
x(0) + x(1) error: type mismatch;
Java Generics にも同等の機能が存在
ワイルドカード
12. Abstract type
型や値を未定義の抽象的な型を定義できる
abstract class Cell {
type T; val value :T
}
未定義の実装を与えることでインスタンス
化
val cell = new Cell { type T = int ; val value = 100 }
println(cell.value) 100
Family polymorphism を実現できる
「クラスの集まり」を継承できる機能 ( 語弊
あり )
まともな説明は Scala のドキュメントを参照
13. Trait
Ruby のモジュールみたいなもの
多重継承可能かつインスタンス化不可
trait Comparable[T <: Comparable[T]] {
def compareTo(o :T) :int
def <(o :T) = compareTo(o) < 0 ...
}
class Person(n :String) ... with Comparable[String] {
def name = n
override def compareTo(o :T) = n compareTo o.name
}
val bar = new Person("Bar"); val foo = new Person("Foo")
println(foo < bar) false
14. まとめ
Structural subtyping
Case class + pattern matching
Lazy value
Implicit conversion(Views)
Singleton type
Generic type
Regular expression pattern
Existential Type
Abstract Type
Trait
Scala サイ
コー
15. 参考資料
公式 Web ページ
http://www.scala-lang.org/
置いてあるドキュメントを読めば大体の機能はわ
かる
ただし最新の機能に追従できてない場合あり
最新機能は処理系に付属の pdf 読んだ方がいいかも