Scala: for-comprehension contexts do not mix

val optionOne = Some(1)
val optionTwo = Some(2)
val eitherOne = Right(1)
val eitherTwo = Right(2)
println(for {
a <- optionOne
b <- optionTwo
} yield a + b)
println(for {
a <- eitherOne
b <- eitherTwo
} yield a + b)
Some(3)
Right(3)
val optionOne = Some(1)
val optionTwo = Some(2)
val eitherOne = Right(1)
val eitherTwo = Right(2)
println(for {
a <- optionOne
b <- eitherTwo
Found: Either[Nothing, Int]
Required: Option[Any]
} yield a + b)

--

--

--

On a continuing learning journey..

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Questions to Ask Yourself Before Searching for App Developers Near Me

Test Through Multiple Interfaces Using Worlds

Ian says:

BlazingSQL Logs

CI/CD of Ruby On Rails using Jenkins

Static members do not get garbage collected through Depedency Injection

Longest Repeating Character Replacement: Leetcode — Blind 75 (Sliding Window)

Binary Search Trees in Go

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Learn

Learn

On a continuing learning journey..

More from Medium

Scala: Thrush combinator

[Scala] Implicit Parameters

Type Classes in Scala 2

Polymorphic functions in Scala