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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:20:39+00:00 2026-06-14T07:20:39+00:00

I have a class structure like this abstract class A class B extends A

  • 0

I have a class structure like this

abstract class A
class B extends A
class C extends A
class D extends A
class E extends A

and I have a collection of the various instances, for example:

val xs = List(new D, new B, new E, new E, new C, new B)

My question is, is there an elegant way to filter out some of the subclasses from the List?

Let’s say I want all instances except B’s and C’s. I can do it with a bunch of isInstanceOf’s, or using collect like this:

val ys = (xs collect {
    case b: B => None
    case c: C => None
    case notBorC => notBorC
}).filter(_ != None).asInstanceOf[List[A]]

This works but it feels awkward, mostly because of the filter and cast. Is there a more elegant way? Less code is preferred, and I’d like to have a solution that doesn’t need to be updated if I add more subclasses of A.

  • 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-14T07:20:40+00:00Added an answer on June 14, 2026 at 7:20 am

    flatMap that shit! (as they say):

    scala> val ys = xs flatMap {
         |   case _: B | _: C => None
         |   case other => Some(other)
         | }
    ys: List[A] = List(D@7ecdc97b, E@2ce07e6b, E@468bb9d1)
    

    In your case you were getting a List[ScalaObject] because ScalaObject is the least upper bound of None, D, and E.

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

Sidebar

Related Questions

I have a similar structure like this: Parent class abstract class parentActions extends sfActions
I have this class structure UserInterface --- abstract Class User extends UserInterface class Teacher
I have a files structure like this: Class (folder): - User.php - Rule.php Scripts
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
I have a class structure like: abstract class A { String a; } class
I have something like this: public abstract class Menu { public Menu() { init();
In C# I have an intrusive tree structure that looks like this: public abstract
I have the following class structure: public abstract class Generic<T extends SuperClass> public class
I have a code first application which defined like this: public abstract class Entity
I have this C# class structure that I would like to refactor to use

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.