命令はオブジェクト
scalaの命令はオブジェクトだそうです。
どういうことかというと、引数でファンクションを渡すことができるということです。
object Timer { def oncePerSecond(callback: () => unit) { while (true) { callback(); Thread sleep 1000 } } def timeFlies() { println("time flies like an arrow...") } def main(args: Array[String]) { oncePerSecond(timeFlies) } }
ruby的には
def once_per_second(&block) while (true) block.call sleep 1 end end time_flies = proc do puts "time flies like an arrow..." end once_per_second(&time_flies)
こんな感じでしょうか。
scalaのやつを見て、Cの関数のポインタを思い出しました。
rubyやってるとすっかり忘れていますけどね。
関数を引数で渡すのに似てるななんて。