Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created May 11, 2011 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/2d1ac3d3673af4b834e3 to your computer and use it in GitHub Desktop.
Save xuwei-k/2d1ac3d3673af4b834e3 to your computer and use it in GitHub Desktop.
2.9.0からcatchの部分にPartialFunctionが書けますよー
//2.9.0以上じゃないと動かないよーーー
//ぱふ のalias
type -->[A,B] = PartialFunction[A,B]
//Throwableを受け取って、エラーメッセージを返す ぱふ
val getErrorMsg: Throwable --> String = {case e:Throwable => e.getMessage }
//関数合成で、エラーメッセージをプリントする関数作成
val printMsg = getErrorMsg.andThen(print)
try{
"hoge" toInt
}catch{
printMsg //2.9.0から ここに ぱふ が書ける
}
// try catch 自体の結果型としてはAny型で、中身はUnit
// エラー起きて、エラーメッセージがprintされる
// エラー時の処理をモジュール化(?)っていうか順番に細かく組み立てて使えていいんじゃね?っていう利点?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment