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 478821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:43:49+00:00 2026-05-13T00:43:49+00:00

The problem is that you want to flatMap a List[Option[T]] to a List[T] :

  • 0

The problem is that you want to flatMap a List[Option[T]] to a List[T] :

val l = List(Some("Hello"), None, Some("World"))

to get:

List(Hello, World)

but there is no nice solution:

l flatMap( o => o)
l flatMap identity[Option[String]]
l flatMap ( x => Option.option2Iterable(identity(x)))
for(x <- l; y <- x) yield y

The obvious solution using the identity function does not work, due to a needed type conversion from Option[T] to Iterable[T]:

l flatMap identity

<console>:6: error: type mismatch;
 found   : A
 required: Iterable[?]
       l flatMap identity

Is there a way around this problem?

Part of the question is why does the type inferencer work differently if implicit type conversions are needed?

(This question came up when this question about the identity function was discussed.)

  • 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-05-13T00:43:49+00:00Added an answer on May 13, 2026 at 12:43 am

    There is no problem with the implicit. If it were a list of list, you wouldn’t be able to pass identity to map or flatMap either. Let’s discuss map instead of flatMap, because it is simpler, so that I can better explain what I think is happening.

    The type of identity is (A) => A. The type of map if (A) => B, where A is known, because it is the type parameter of the object (ie, in a List[String], it is String). Neither identity‘s A nor map‘s B is known.

    Now, if we used the left side of map‘s type to infer the left side of identity, then we could use the right side of identity to infer the right side of map. See a cycle? map‘s type to identity‘s type to map‘s type again. I’m pretty sure the type inferencer avoids cycles, otherwise it could be stuck in an infinite loop.

    In fact, one can look at page 355 of Odersky et al’s Programming in Scala the details of the type inferencer. In a method application m(arg), it does the following steps:

    1. It checks whether method m (map, in our case) has a known type (no, we don’t know B).
    2. It checks if the argument arg (identity in our case) has a known type (no, we don’t know A).
    3. It bails out.

    So, you have to provide one type. For example:

    l.flatMap[String](identity)
    l.flatten[String]
    

    On Scala 2.8 the type inferencer is a little bit cleverer, and can handle the second command without passing the type explicitly.

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

Sidebar

Related Questions

My problem is simple: I have a long list of elements that I want
I have a problem that I want to set and get Arraylists from a
I have a problem that I want to use Facebook SDK in Android but
with my RCP program I have the problem that I want to have more
My problem is that I want to have a tab bar view with its
My problem is that I want to redirect via JavaScript to a directory above.
My problem is that I want to rotate the UIImageView of a UIButton .
My Problem is that I want to check if the Arrow up or down
The problem is that I want to output Mathematica compatible floating point numbers. The
How can i stack images onto eachother? My problem is that i want the

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.