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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:38:50+00:00 2026-06-12T08:38:50+00:00

I have a case class like the following: // parent class sealed abstract class

  • 0

I have a case class like the following:

// parent class
sealed abstract class Exp()

// the case classes I want to match have compatible constructors
case class A (a : Exp, b : Exp) extends Exp
case class B (a : Exp, b : Exp) extends Exp
case class C (a : Exp, b : Exp) extends Exp

// there are other case classes extending Exp that have incompatible constructor, e.g.
//     case class D (a : Exp) extends Exp
//     case class E () extends Exp
// I don't want to match them

I want to match:

var n : Exp = ...
n match {
    ...
    case e @ A (a, b) => 
        foo(e, a)
        foo(e, b)
    case e @ B (a, b) => 
        foo(e, a)
        foo(e, b)
    case e @ C (a, b) => 
        foo(e, a)
        foo(e, b)
    ...
}

def foo(e : Exp, abc : Exp) { ... }

is there a way to merge that three cases into a single case (without adding an intermediate parent class to A,B,C)? I can’t change the definition of A, B, C, or Exp. Some sort of:

var n : Exp = ...
n match {
    ...
    case e @ (A | B | C) (a, b) => // invalid syntax
        foo(e, a)
        foo(e, b)
    ...
}

which obviously does not work, and neither do:

var n : Exp = ...
n match {
    ...
    case e @ (A (a, b) | B (a, b) | C (a, b)) => // type error
        foo(e, a)
        foo(e, b)
    ...
}
  • 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-12T08:38:52+00:00Added an answer on June 12, 2026 at 8:38 am

    While the following “solution” is really just a different way of writing what you already have, it might help if you need to use the same match in more than one place and want to avoid code duplication.

    The following custom unapply:

    object ExpABC {
        def unapply(e:Exp):Option[(Int, Int)] = e match {
            case A(a, b) => Some(a, b)
            case B(a, b) => Some(a, b)
            case C(a, b) => Some(a, b)
            case _ => None
        }
    }
    

    allows you to write

    n match {
        case e @ ExpABC(a, b) =>
            println(e)
            println(a)
            println(b)
    }
    

    This way you don’t need to modify the original classes at all. I’m not aware of a better way to do this that doesn’t involve modifying the A/B/C classes, but I’m eager to learn @ Stackoverflow 😉

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

Sidebar

Related Questions

Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Assuming that I have a Scala case class like the following: case class Item(
I have the following classes: case class Vec2(x: Int, y: Int) { def +(other:
I have a hierarchy like the following: case class A(val a: Long, val b:
I have the following use case: There's a class called Template and with that
I have the following test case: @ContextConfiguration(/spring/test-context.xml) @TransactionConfiguration(transactionManager=txManager) @Transactional() public class MyEntityDaoTestCase extends AbstractJUnit4SpringContextTests
I have an enum class like the following: public enum Letter { OMEGA_LETTER(Omega), GAMMA_LETTER(Gamma),
Lets say I have the following class Parent { protected: virtual void overrideMe() =
I have the following case: public class GeoLocation { public double Longitude { get;
For example is it better to have: public class Case : EntityIdentifiable { public

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.