Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit feb6909

Browse files
committed
Get a standard Either
1 parent 4cd57d8 commit feb6909

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: src/main/scala/com/ckkloverdos/maybe/Maybe.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ object MaybeOption {
138138
/**
139139
* A Maybe that can be either a Just or a Failed. So, this is like Scala's Either.
140140
*/
141-
sealed trait MaybeEither[+A] extends Maybe[A]
141+
sealed trait MaybeEither[+A] extends Maybe[A] {
142+
def toEither: Either[Throwable, A]
143+
}
142144

143145
object MaybeEither {
144146
def apply[A](x: A): MaybeEither[A] = Maybe(x) match {
@@ -181,6 +183,8 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
181183
def toList = List(get)
182184
def toStream = Stream.cons(get, Stream.Empty)
183185

186+
def toEither: Either[Throwable, A] = Right(get)
187+
184188
def isJust = true
185189
def isFailed = false
186190
def isNoVal = false
@@ -292,6 +296,8 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
292296
def toList = Maybe.MaybeEmptyList
293297
def toStream = Stream.Empty
294298

299+
def toEither: Either[Throwable, Nothing] = Left(exception)
300+
295301
def getOr[B >: Nothing](b: B) = b
296302

297303
def fold[T](onJust: (Nothing) T)(onNoVal: T)(onFailed: (Failed) T) = onFailed(this)

0 commit comments

Comments
 (0)