-
-
Save xuwei-k/2d1ac3d3673af4b834e3 to your computer and use it in GitHub Desktop.
2.9.0からcatchの部分にPartialFunctionが書けますよー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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