Scalaでは、updateという特別なメソッドがあります。
これは、array(idx) = x のような形で呼び出され、指定のインデックスの値を書き換えます。
このupdateに独自のロジックを拡張する方法を見ていきます。
updateメソッドを独自実装する
def main(arts: Array[String]) : Unit = { val sample = new UpdateSample(Array(1, 2, 3)) sample(0) = 5 // Same:sample.update(0, 5) println(sample(0)) // =>10 } class UpdateSample(var arr : Array[Int]) { def apply(n :Int) = arr(n) // 与えられた引数を2倍にしてSetする def update(idx: Int, value :Int) = { arr(idx) = value * 2 } }