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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:42:30+00:00 2026-06-18T01:42:30+00:00

I have the following data model which I’m going to do pattern matching against

  • 0

I have the following data model which I’m going to do pattern matching against later:

abstract class A
case class C(s:String) extends A

abstract class B extends A
case class D(i:Int) extends B
case class E(s:Int, e:Int) extends B

A is the abstract super type of the hierarchy. C is a concrete subclass of A. Other concrete subclasses of A are subclasses of B which is in turn a subclass of A.

Now if I write something like this, it works:

def match(a:A) a match {
   a:C => println("C")
   a:B => println("B")
}

However, in a for loop I cannot match against B. I assume that I need a constructor pattern, but since B is abstract, there is no constructor pattern for B.

val list:List[A] = List(C("a"), D(1), E(2,5), ...)

for (b:B <- list) println(b)  // Compile error
for (b@B <- list) println(b)  // Compile error

Here, I would like to print only B instances. Any workaround for this case?

  • 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-18T01:42:32+00:00Added an answer on June 18, 2026 at 1:42 am

    You can use collect:

    list.collect { case b: B => println(b) }
    

    If you want to better undertand this, I recommend to read about partial functions. Here for example.

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

Sidebar

Related Questions

I have a domain data model which returns a class such as the following:
I have the following core data model with two entities: entity item which holds
I have the following problem: In my data model I have a class called
I have this following view which I get data from a model and thereafter
I have the following Core Data model, in which ProFormaPeriod is a subclass of
I have the following data model: I am writing a WCF service that needs
I have following data structure (simplified): A giant list of the class TestClass public
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
I have the following model: public class Product { public int Id { get;
I have a Core Data model in which a Task entity includes an optional

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.