Created
November 1, 2015 00:29
-
-
Save missingfaktor/76eab2813cfae1a0eadb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package sudoku.errors | |
| import scala.language.higherKinds | |
| import scala.reflect.ClassTag | |
| object Modes { | |
| trait Mode { | |
| type Result[_, _] | |
| def run[E : ClassTag, A](a: => A): Result[E, A] | |
| } | |
| object Mode { | |
| implicit val exceptionsMode = new Mode { | |
| type Result[E, A] = A | |
| def run[E : ClassTag, A](a: => A): A = a | |
| } | |
| implicit val optionMode = new Mode { | |
| type Result[E, A] = Option[A] | |
| def run[E : ClassTag, A](a: => A): Option[A] = { | |
| try { | |
| Some(a) | |
| } catch { | |
| case _: E => None | |
| } | |
| } | |
| } | |
| implicit val eitherMode = new Mode { | |
| type Result[E, A] = Either[E, A] | |
| def run[E : ClassTag, A](a: => A): Either[E, A] = { | |
| try { | |
| Right(a) | |
| } catch { | |
| case ex: E => Left(ex) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| object Runner { | |
| import Modes._ | |
| def parseInt(s: String)(implicit mode: Mode): mode.Result[NumberFormatException, Int] = { | |
| mode.run { | |
| s.toInt | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment