-
-
Save halcat0x15a/2383148 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
trait Container[+R, +C] { | |
def map[A](f: C => A): Container[R, A] | |
def flatMap[RR >: R, A](f: C => Container[RR, A]): Container[RR, A] | |
def toResult[RR >: R](implicit ev: C <:< RR): Result[RR] | |
} | |
case class Calculating[+R, +C](element: C) extends Container[R, C] { | |
def map[A](f: C => A): Container[R, A] = Calculating(f(element)) | |
def flatMap[RR >: R, A](f: C => Container[RR, A]): Container[RR, A] = f(element) | |
def toResult[RR >: R](implicit ev: C <:< RR): Result[RR] = Result(element) | |
} | |
case class Result[+R](element: R) extends Container[R, Nothing] { | |
def map[A](f: Nothing => A): Container[R, A] = this | |
def flatMap[RR >: R, A](f: Nothing => Container[RR, A]): Container[RR, A] = this | |
def get: R = element | |
def toResult[RR >: R](implicit ev: Nothing <:< RR): Result[RR] = this | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment