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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:34:14+00:00 2026-05-13T09:34:14+00:00

I might be approaching this the wrong way, but I’d like to have an

  • 0

I might be approaching this the wrong way, but I’d like to have an object like this:

class MyDataStructure {
  def myClone = {
    val clone = new MyDataStructure
    // do stuff to make clone the same as this
    ...
    clone
  }
}

class MyDataStructureExtended(val foo: String) extends MyDataStructure

Then:

val data = MyDataStructureExtended
val dataClone = data.clone
println(dataClone.foo)

So, the problem is that dataClone is of type MyDataStructure, not MyDataStructureExtended as I’d hoped.

I thought about adding a type T to the super class, that the subclass can specify (e.g. itself), but that didn’t seem very promising.

  • 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-05-13T09:34:14+00:00Added an answer on May 13, 2026 at 9:34 am

    Assuming you want to minimize amount of ceremony in the subclasses, here is my suggestion:

    class A extends Cloneable {
      protected[this] def myCloneImpl[T] = {
        val justLikeMe = this.clone
        // copy values and such.
        // Note that the Object.clone method already made a shallow copy, but you may want
        // to deepen the copy or do other operations.
        justLikeMe.asInstanceOf[T]
      }
      def myClone = myCloneImpl[A]
    }
    
    class B extends A {
      override def myClone = myCloneImpl[B]
    }
    

    By extending java.lang.Cloneable and calling the Object.clone method, you ensure that your runtime type is the same as the object being cloned. The static type is coerced with a type-cast (asInstanceOf[T]). You will need to override the myClone method in each subclass and specify the type, but it should be a one-liner.

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

Sidebar

Related Questions

I might be approaching this all wrong but... I have a linux kernel device
I think I might be approaching this in the wrong way, so I would
I might be approaching this problem completely wrong, but I'm still a bit new
This might seem like a very easy question for some of you folks, but
I might be doing this wrong overall, but maybe someone will be able to
I might have a flawed understanding of what shared_examples_for should do, but hear me
It might look like a stupid question, but I recently realized that .NET implementation
This might be a simple question but I've searched and searched and can't find
Might be the same issue as this previuos question: WCF Proxy but not sure...
Might sound like a stupid question I guess but rubyonrails.org is just an advert

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.