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

  • SEARCH
  • Home
  • 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 9147571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:01:17+00:00 2026-06-17T11:01:17+00:00

I have this issue that I have to work around every time. I can’t

  • 0

I have this issue that I have to work around every time. I can’t map over something that is contained within a Future using a for comprehension.

Example:

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

val f = Future( List("A", "B", "C") )
for {
  list <- f
  e <- list
} yield (e -> 1)

This gives me the error:

 error: type mismatch;
 found   : List[(String, Int)]
 required: scala.concurrent.Future[?]
              e <- list
                ^

But if I do this it works fine:

f.map( _.map( (_ -> 1) ) )

Should i not be able to do this by using a for comprehension, is the reason it works in my other example that I do not flatmap? I’m using Scala 2.10.0.

  • 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-17T11:01:18+00:00Added an answer on June 17, 2026 at 11:01 am

    Well, when you have multiple generators in a single for comprehension, you are flattening the resulting type. That is, instead of getting a List[List[T]], you get a List[T]:

    scala> val list = List(1, 2, 3)
    list: List[Int] = List(1, 2, 3)
    
    scala> for (a <- list) yield for (b <- list) yield (a, b)
    res0: List[List[(Int, Int)]] = List(List((1,1), (1,2), (1,3)), List((2,1
    ), (2,2), (2,3)), List((3,1), (3,2), (3,3)))
    
    scala> for (a <- list; b <- list) yield (a, b)
    res1: List[(Int, Int)] = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3),
    (3,1), (3,2), (3,3))
    

    Now, how would you flatten a Future[List[T]]? It can’t be a Future[T], because you’ll be getting multiple T, and a Future (as opposed to a List) can only store one of them. The same problem happens with Option, by the way:

    scala> for (a <- Some(3); b <- list) yield (a, b)
    <console>:9: error: type mismatch;
     found   : List[(Int, Int)]
     required: Option[?]
                  for (a <- Some(3); b <- list) yield (a, b)
                                       ^
    

    The easiest way around it is to simply nest multiple for comprehensions:

    scala> for {
         |   list <- f
         | } yield for {
         |   e <- list
         | } yield (e -> 1)
    res3: scala.concurrent.Future[List[(String, Int)]] = scala.concurrent.im
    pl.Promise$DefaultPromise@4f498585
    

    In retrospect, this limitation should have been pretty obvious. The problem is that pretty much all examples use collections, and all collections are just GenTraversableOnce, so they can be mixed freely. Add to that, the CanBuildFrom mechanism for which Scala has been much criticized makes it possible to mix in arbitrary collections and get specific types back, instead of GenTraversableOnce.

    And, to make things even more blurry, Option can be converted into an Iterable, which makes it possible to combine options with collections as long as the option doesn’t come first.

    But the main source of confusion, in my opinion, is that no one ever mentions this limitation when teaching for comprehensions.

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

Sidebar

Related Questions

Heyy can somebody help me ? i have this issue that still do know
I have this issue with ComboBox that uses IEnumerable<Brush> as ItemsSource; the problem lies
I have never come across this issue but most recently I noticed that a
I have an issue that seems like very flaky behavour, is this a problem
OK so I have an xslt issue that I cannot seem to solve. This
I am having this issue. I have a script that checks if variable exists,
I am having some problems figuring out this issue. I have a server that
I know that there are some threads have a similar issue with this thread.
Wondering if anyone has a work around for this issue. I've got a markup
I have an eventHandler that is trigered every time a node is inserted in

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.