Scala

既存クラスにメソッド追加

More than 5 years have passed since last update.

http://www.ne.jp/asahi/hishidama/home/tech/scala/def.html


例1: Doubleクラスに単位変換メソッドを追加

implicit def convert(d:Double) = new {

//ミリ→ポイント変換
def mmToPoint = {d * 2.835}
//ポイント→ミリ変換
def pointToMm = {d * 0.353}
}


val originalMm = 100d
val point = originalMm.mmToPoint

val originalPoint = 100d
val mm = originalPoint.pointToMm