Contenu connexe
Similaire à 頑張りすぎないScala (20)
頑張りすぎないScala
- 27. コンパイルされるとこんな感じ(イメージ)
def hello(names: Seq[String]): String = {
try {
names.foreach(checkHelloArguments)
} catch {
case e: NonLocalReturnControl[String] => return e.value
}
...
}
private def checkHelloArguments(str: String): Unit = {
if(str.isEmpty) throw new NonLocalReturnControl[String]("")
}
- 37. Eitherの使い方
val result = findUser()
// パターンマッチで結果を取り出す
result match {
case Right(user) => ... // 成功時の処理
case Left(e) => ... // エラー時の処理
}