Last active
January 8, 2020 12:06
-
-
Save nnao45/5408b43b8c9d1b81960fc47445bcd4e0 to your computer and use it in GitHub Desktop.
Either vs Option
This file contains 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 state.sample01 | |
case class V(value: Int) | |
object Main extends App { | |
val result1: Either[V, Unit] = Right(Unit) | |
val result2: Either[V, Unit] = Right(Unit) | |
val result3: Either[V, Unit] = Left(V(1)) | |
val result4: Either[V, Unit] = Left(V(2)) | |
val ret1 = (for { | |
_ <- result1 | |
_ <- result2 | |
_ <- result3 | |
_ <- result4 | |
} yield ()).left.get | |
assert(ret1.value == 1) | |
val result5: Option[V] = None | |
val result6: Option[V] = None | |
val result7: Option[V] = Some(V(2)) | |
val result8: Option[V] = None | |
val ret2 = result5 | |
.getOrElse( | |
result6 | |
.getOrElse( | |
result7 | |
.getOrElse( | |
result8.get | |
) | |
)) | |
assert(ret2.value == 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment