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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:24:24+00:00 2026-06-13T09:24:24+00:00

Say I have following code: object ForComprehension { def generateSomeSeq(n: Int): List[Int] = (1

  • 0

Say I have following code:

object ForComprehension {
  def generateSomeSeq(n: Int): List[Int] = (1 to n).toList
  def square(n: Int): List[Int] =
    for (x <- generateSomeSeq(n)) yield x * x
}

It all works fine. But for some reason I modify the result of first method to Set instead of List:

object ForComprehension {
  def generateSomeSeq(n: Int): Set[Int] = (1 to n).toSet
  def square(n: Int): List[Int] =
    for (x <- generateSomeSeq(n)) yield x * x
}

The scala compiler complains there’s a type mismatch in first generator of for comprehension. Although it makes sense, I still expect it works in both cases. After all, there’s no visual clue in for comprehension that it expects a List instead of a Set. I need to read both signatures to diagnose the type mismatch error.

Or I shouldn’t expect such syntactical locality for for comprehension in the first place, should I?

  • 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-13T09:24:26+00:00Added an answer on June 13, 2026 at 9:24 am

    The for comprehension in your example is really just syntactic sugar for a call to map and the return type of map is indeed determined by the type it is called on. More precisely, it is determined by the implicit CanBuildFrom that is available. So to make your code compile, you can convert the for comprehension to a map call and explicitly pass the usually implicit parameter:

    object ForComprehension {
      def generateSomeSeq(n: Int): Set[Int] = (1 to n).toSet
      def square(n: Int): List[Int] = 
        generateSomeSeq(n).map(x => x * x)(scala.collection.breakOut)
    }
    

    About how breakOut works, see this question.

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

Sidebar

Related Questions

Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
Say I have the following java code: Object myObj = new ArrayList(); List myList
Say I have the following code: struct date { int day; int month; int
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Say I have the following code: private Integer number; private final Object numberLock =
I have the following code with private static members. All of these classes say
Let's say I have the following code static class ... { static object myobj
Let's say I have the following code... StringBuilder sb = new StringBuilder(); for (int
Lets say I have the following code: for (Object obj : Node.getIterable()) { //Do

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.