Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:31:06+00:00 2026-06-04T08:31:06+00:00

I can use an = in a scala for-comprehension (as specified in section 6.19

  • 0

I can use an = in a scala for-comprehension (as specified in section 6.19 of the SLS) as follows:

Option

Suppose I have some function String => Option[Int]:

scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None }
intOpt: (s: String)Option[Int]

Then I can use it thus

scala> for {
   |     str <- Option("1")
   |     i <- intOpt(str)
   |     val j = i + 10    //Note use of = in generator
   |   }
   |   yield j
res18: Option[Int] = Some(11)

It was my understanding that this was essentially equivalent to:

scala> Option("1") flatMap { str => intOpt(str) } map { i => i + 10 } map { j => j }
res19: Option[Int] = Some(11)

That is, the embedded generator was a way of injecting a map into a sequence of flatMap calls. So far so good.

Either.RightProjection

What I actually want to do: use a similar for-comprehension as the previous example using the Either monad.

However, if we use it in a similar chain, but this time using the Either.RightProjection monad/functor, it doesn’t work:

scala> def intEither(s: String): Either[Throwable, Int] = 
  |      try { Right(s.toInt) } catch { case x => Left(x) }
intEither: (s: String)Either[Throwable,Int]

Then use:

scala> for {
 | str <- Option("1").toRight(new Throwable()).right
 | i <- intEither(str).right //note the "right" projection is used
 | val j = i + 10
 | }
 | yield j
<console>:17: error: value map is not a member of Product with Serializable with Either[java.lang.Throwable,(Int, Int)]
              i <- intEither(str).right
                ^

The issue has something to do with the function that a right-projection expects as an argument to its flatMap method (i.e. it expects an R => Either[L, R]). But modifying to not call right on the second generator, it still won’t compile.

scala>  for {
 |        str <- Option("1").toRight(new Throwable()).right
 |        i <- intEither(str) // no "right" projection
 |          val j = i + 10
 |      }
 |      yield j
<console>:17: error: value map is not a member of Either[Throwable,Int]
              i <- intEither(str)
                            ^

Mega-Confusion

But now I get doubly confused. The following works just fine:

scala> for {
 |       x <- Right[Throwable, String]("1").right
 |       y <- Right[Throwable, String](x).right //note the "right" here
 |     } yield y.toInt
res39: Either[Throwable,Int] = Right(1)

But this does not:

scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
<console>:14: error: type mismatch;
 found   : Either.RightProjection[Throwable,String]
 required: Either[?,?]
              Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
                                                                                             ^

I thought these were equivalent

  • What is going on?
  • How can I embed an = generator in a for comprehension across an Either?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:31:08+00:00Added an answer on June 4, 2026 at 8:31 am

    The fact that you cannot embed the = in the for-comprehension is related to this issue reported by Jason Zaugg; the solution is to Right-bias Either (or create a new data type isomorphic to it).

    For your mega-confusion, you expanded the for sugar incorrectly. The desugaring of

    for {
      b <- x(a)
      c <- y(b)
    } yield z(c)
    

    is

    x(a) flatMap { b =>
     y(b) map { c =>
      z(c) }} 
    

    and not

    x(a) flatMap { b => y(b)} map { c => z(c) }
    

    Hence you should have done this:

    scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right map { y => y.toInt } }
    res49: Either[Throwable,Int] = Right(1)
    

    More fun about desugaring (the `j = i + 10` issue)

    for {
      b <- x(a)
      c <- y(b)
      x1 = f1(b)
      x2 = f2(b, x1)
      ...
      xn = fn(.....)
      d <- z(c, xn)
    } yield w(d)
    

    is desugared into

    x(a) flatMap { b =>
      y(b) map { c =>
        x1 = ..
        ...
        xn = ..
        (c, x1, .., xn) 
      } flatMap { (_c1, _x1, .., _xn) =>
        z(_c1, _xn) map w }}
    

    So in your case, y(b) has result type Either which doesn’t have map defined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Scala Option[T] . If the value is Some(x) I want to
Given the string val path = /what/an/awesome/path how can I use Scala to create
I can use -d option to put files into a database. mongofiles -d test
I can use GD or something to draw user-selected shapes by writing some lines
I can use the below to get the query string. var query_string = request.query;
We can use reflection to create the objects. Let's say I have a class
Using Scala 2.7.7: If I have a list of Options, I can flatten them
Everyone who's done any web application development in Scala knows that you can use
looks like when scala 2.8.0 is out, we can use nested @annotations in our
How can I use a for-comprehension that returns something I can assign to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.