11th Tue
Rubyスタイルの後置rescueをScalaで実現する
中置記法と暗黙変換のコンビネーションでRubyの後置rescue的なものをScalaで実装してみました。
ruby>>
class Rescueable[A](a: => A) {
def rescue[B](b: => B) = try{a}catch{case _ => b}
}
implicit def anyToRescueable[A](a: => A) = new Rescueable(a)
println("12341234".toLong rescue 0) // => 12341234
println("1234x1234".toLong rescue 0) // => 0
<<--
call-by-nameがポイントです。
Update
bがcall-by-nameになっていなかったので修正しました。
posted by
genki on Tue 11 May 2010 at 11:03 with 0 comments