Skip to content

Instantly share code, notes, and snippets.

@nnao45
Last active January 8, 2020 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnao45/5408b43b8c9d1b81960fc47445bcd4e0 to your computer and use it in GitHub Desktop.
Save nnao45/5408b43b8c9d1b81960fc47445bcd4e0 to your computer and use it in GitHub Desktop.
Either vs Option
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