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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:56:40+00:00 2026-06-03T02:56:40+00:00

I have two case classes that inherit from an abstract base class. I want

  • 0

I have two case classes that inherit from an abstract base class. I want to define some methods on the abstract base class that use the copy methods on the inheriting case classes (and so return an instance of the child class.) Is there a way to do this using self types?

Example code:

abstract class BaseClass(a: String, b: Int) {
  this: case class => //not legal, but I'm looking for something similar

  def doubleB(newB: Int) = this.copy(b = b * 2) //doesn't work because BaseClass has no copy
}

case class HasC(a: String, b: Int, c: Boolean) extends BaseClass(a, b) {
  def doesStuffWithC(newC: Boolean) = {
    ...
  }
}

case class HasD(a: String, b: Int, D: Double) extends BaseClass(a, b) {
  def doesStuffWithD(newD: Double) = {
    ...
  }
}

I’ve figured out how to get the result I want thanks to this question:
How to use Scala's this typing, abstract types, etc. to implement a Self type?
but it involves adding a makeCopy method to BaseClass and overriding it with a call to copy in each of the child case classes, and the syntax (especially for the Self type) is fairly confusing. Is there a way to do this with Scala’s built in self typing?

  • 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-03T02:56:40+00:00Added an answer on June 3, 2026 at 2:56 am

    You can’t do what you want because copy needs to know about all the possible parameters. So even if case classes inherited from Copyable, it wouldn’t be the copy you needed. Also, if you’re going to keep the types straight, you’ll be thwarted by Scala’s lack of a “MyType“. So you can’t just extend a base class. However, you could add an abstract method and type annotation:

    abstract class BaseClass[C <: BaseClass[_]](a: String, b: Int) {
      def setB(b0: Int): C
      def doubleB(b0: Int) = setB(b0*2)
    }
    case class HasC(a: String, b: Int, c: Boolean) extends BaseClass[HasC](a,b) {
      def setB(b0: Int) = this.copy(b = b0)
      def doesStuffWithC(c0: Boolean) = doubleB(if (c0) b else -b).copy(c = c0)
    }
    

    And then you can:

    scala> HasC("fish",1,false).doesStuffWithC(true)
    res47: HasC = HasC(fish,2,true)
    

    This extra work will be worth it if you have a lot of shared functionality that depends on the ability to copy just b (either many methods, or a small number of complicated methods)–that is, this solves the DRY issue. If instead you want to abstract over HasC and other derived classes, you can either use BaseClass[_] or add yet another level that defines setB(b0: Int): BaseBase or simply forget the type parameterization and use BaseClass as the return type (but recognize that HasC cannot use BaseClass methods and still retain its type identity).

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

Sidebar

Related Questions

I have two classes: MainPanel (where I define some methods) MainThread (where I do
I want to use data contract class as base class of some other classes.
Let us assume we have the two following classes: abstract case class MyParent(param: Int)
My use case is this... I have a project that has two production branches.
I have two classes that I use to access two different tables in my
I have two classes that can act as a delegate of a third class,
If I have for example two classes A and B , such that class
I have two classes namely classA and classB which both inherit from class1 .
I Have two problem in this Case : I want to pass a JSON
I want to have two EntityManagerFactories ( org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in this case), with each of

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.