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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:01:56+00:00 2026-06-01T22:01:56+00:00

Imagine a class A that is abstract and has a set of case classes.

  • 0

Imagine a class A that is abstract and has a set of case classes. We don’t know how many set classes are or even the name those case classes have.

abstract class A(field: String)
//Example of a case class that extends A
case class B(f: String) extends A(f)

Now I have this:

a match {
    case B(f) => println("f")
}

And I want to pass the case class type by an argument into a method. The reason I want this is because I will configure a set of rules in a file. And I want to load those rules and use pattern matching with some info those rules provide. I want to do something like this:

def printer (a: A, B: A) = {
   a match{
       case B(f) => println("f")
   }
}

Is this possible?

If it isn’t that easy can I use an abstract class in pattern matching? It would be perfect if I could simply use the abstract class since it has the main structure of all case classes.

EDIT:

Forgot to mention that case classes can have different arguments so it would be good to use something based on class A (since I can do my pattern matching with the field only)

  • 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-01T22:02:00+00:00Added an answer on June 1, 2026 at 10:02 pm

    Not like you tried it. But if you use a Manifest as context bound, you can make it work:

    scala> trait Foo
    defined trait Foo
    
    scala> case class Bar(baz: String) extends Foo
    defined class Bar
    
    scala> def boo[A <: Foo : Manifest](foo: Foo) =
         |   if (foo.getClass isAssignableFrom manifest[A].erasure) "foo" else "boo"
    boo: [A <: Foo](foo: Foo)(implicit evidence$1: Manifest[A])java.lang.String
    
    scala> boo[Foo](Bar(""))
    res0: java.lang.String = boo
    
    scala> boo[Bar](Bar(""))
    res1: java.lang.String = foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having in mind the abstract factory pattern, imagine that you have a class hierarchy
Imagine I have abstract base class Shape , with derived classes Circle and Rectangle
Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
Imagine that I have a nice Deck class, in the best OO fashion. It
Imagine I have a C++ class Foo and a class Bar which has to
In C#, I have a class hierarchy with a couple of abstract base classes
We have a set of about two dozen classes that inherit from a base
Imagine that I have a Debtor class. With Hibernate, I will define the class
Imagine I have a abstract FriendEvent model which has several different concrete implementations, ie.
In Python, I have a Graph class that has a dictionary of vertex objects.

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.