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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:40:50+00:00 2026-05-26T11:40:50+00:00

Is it possible in scala to specialize on the conditions inside an if within

  • 0

Is it possible in scala to specialize on the conditions inside an if within a for comprehension? I’m thinking along the lines of:

val collection: SomeGenericCollection[Int] = ...

trait CollectionFilter
case object Even extends CollectionFilter
case object Odd extends CollectionFilter

val evenColl = for { i <- collection if(Even) } yield i
//evenColl would be a SomeGenericEvenCollection instance

val oddColl = for { i <- collection if(Odd) } yield i
//oddColl would be a SomeGenericOddCollection instance

The gist is that by yielding i, I get a new collection of a potentially different type (hence me referring to it as “specialization”)- as opposed to just a filtered-down version of the same GenericCollection type.

The reason I ask is that I saw something that I couldn’t figure out (an example can be found on line 33 of this ScalaQuery example. What it does is create a query for a database (i.e. SELECT ... FROM ... WHERE ...), where I would have expected it to iterate over the results of said query.

  • 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-26T11:40:51+00:00Added an answer on May 26, 2026 at 11:40 am

    So, I think you are asking if it is possible for the if statement in a for-comprehension to change the result type. The answer is “yes, but…”.

    First, understand how for-comprehensions are expanded. There are questions here on Stack Overflow discussing it, and there are parameters you can pass to the compiler so it will show you what’s going on.

    Anyway, this code:

    val evenColl = for { i <- collection if(Even) } yield i
    

    Is translated as:

    val evenColl = collection.withFilter(i => Even).map(i => i)
    

    So, if the withFilter method changes the collection type, it will do what you want — in this simple case. On more complex cases, that alone won’t work:

    for {
      x <- xs
      y <- ys
      if cond
    } yield (x, y)
    

    is translated as

    xs.flatMap(ys.withFilter(y => cond).map(y => (x, y)))
    

    In which case flatMap is deciding what type will be returned. If it takes the cue from what result was returned, then it can work.

    Now, on Scala Collections, withFilter doesn’t change the type of the collection. You could write your own classes that would do that, however.

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

Sidebar

Related Questions

Possible Duplicate: val and object inside a scala class? Is there a substantive difference
The following is possible in Scala: scala> val l = List l: scala.collection.immutable.List.type =
Possible Duplicate: Scala “<-” for comprehension Could someone please explain how the <- operator
In Scala, is it possible for a trait to reference a named constructor argument
Possible Duplicate: What's the (hidden) cost of lazy val? (Scala) Scala allows the definition
In Scala it's possible to use the annotation @BeanProperty to automatically generate getters and
In Scala, is it possible to get the string representation of a type at
Possible Duplicate: What is the rationale behind having companion objects in Scala? Thanks for
Is it possible to extend multiple classes in Scala. For example if I have
Is it possible to create types like e.g. String(20) in scala? The aim would

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.