스칼라 언어가 sugar syntax가 많이 있는 반면, Option에 대한 에러 처리 부분은 친절하지 않다. Option은 Some과 None만 있는 ADT이지 Throwable이나 Exception와 값을 갖지 못한다.
따라서 scalaz의 Either 라이브러리(Either[Exception, Int]) 또는 disjunction(Exception \/ Int) 라이브러리를 살펴봐야 한다.
(나중에 공부해야지)
http://eed3si9n.com/learning-scalaz/Either.html
(참고) http://danielwestheide.com/blog/2013/01/02/the-neophytes-guide-to-scala-part-7-the-either-type.html
import scala.util.control.Exception.catching def handling[Ex <: Throwable, T](exType: Class[Ex])(block: => T): Either[Ex, T] = catching(exType).either(block).asInstanceOf[Either[Ex, T]]
http://appliedscala.com/blog/2016/scalaz-disjunctions
def queryNextNumber: Exception \/ Long = { val source = Math.round(Math.random * 100) if (source <= 60) \/.right(source) else \/.left(new Exception("The generated number is too big!")) }
'scala' 카테고리의 다른 글
[scala] Vector (0) | 2016.10.28 |
---|---|
zeppelin 과 spark 연동 (0) | 2016.10.28 |
List에 적용하는 for yield 예시 2 (0) | 2016.10.24 |
[scala] 정수의 산술 연산시 IllegalFormatConversionException 발생 (0) | 2016.10.19 |
[scala] 특이한 Iterator (0) | 2016.10.19 |