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

  • Home
  • SEARCH
  • 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 8371063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:04:28+00:00 2026-06-09T14:04:28+00:00

A case classes copy() method is supposed to make an identical copy of the

  • 0

A case classes copy() method is supposed to make an identical copy of the instance, plus replacing any fields by name. This seems to fail when the case class has type parameters with manifests. The copy loses all knowledge of the types of its parameters.

case class Foo[+A : Manifest](a: A) {
  // Capture manifest so we can observe it
  // A demonstration with collect would work equally well
  def myManifest = implicitly[Manifest[_ <: A]]
}

case class Bar[A <: Foo[Any]](foo: A) {
  // A simple copy of foo
  def fooCopy = foo.copy()
}

val foo = Foo(1)
val bar = Bar(foo)

println(bar.foo.myManifest)     // Prints "Int"
println(bar.fooCopy.myManifest) // Prints "Any"

Why does Foo.copy lose the manifest on the parameters and how do I make it retain it?

  • 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-09T14:04:30+00:00Added an answer on June 9, 2026 at 2:04 pm

    Several Scala peculiarities interact to give this behavior. The first thing is that Manifests are not only appended to the secret implicit parameter list on the constructor but also on the copy method. It is well known that

    case class Foo[+A : Manifest](a: A)

    is just syntactic sugar for

    case class Foo[+A](a: A)(implicit m: Manifest[A])

    but this also affects the copy constructor, which would look like this

    def copy[B](a: B = a)(implicit m: Manifest[B]) = Foo[B](a)(m)

    All those implicit ms are created by the compiler and sent to the method through the implicit parameter list.

    This would be fine as long as one was using the copy method in a place where the compiler knew Foos type parameter. For example, this will work outside of the Bar class:

    val foo = Foo(1)
    val aCopy = foo.copy()
    println(aCopy.myManifest) // Prints "Int"
    

    This works because the compiler infers that foo is a Foo[Int] so it knows that foo.a is an Int so it can call copy like this:

    val aCopy = foo.copy()(manifest[Int]())

    (Note that manifest[T]() is a function that creates a manifest representation of the type T, e.g. Manifest[T] with a capital “M”. Not shown is the addition of the default parameter into copy.) It also works within the Foo class because it already has the manifest that was passed in when the class was created. It would look something like this:

    case class Foo[+A : Manifest](a: A) {
      def myManifest = implicitly[Manifest[_ <: A]]
    
      def localCopy = copy()
    }
    
    val foo = Foo(1)
    println(foo.localCopy.myManifest) // Prints "Int"
    

    In the original example, however, it fails in the Bar class because of the second peculiarity: while the type parameters of Bar are known within the Bar class, the type parameters of the type parameters are not. It knows that A in Bar is a Foo or a SubFoo or SubSubFoo, but not if it is a Foo[Int] or a Foo[String]. This is, of course, the well-known type erasure problem in Scala, but it appears as a problem here even when it doesn’t seem like the class is doing anything with the type of foos type parameter. But it is, remember there is a secret injection of a manifest every time copy is called, and those manifests overwrite the ones that were there before. Since the Bar class has no idea was the type parameter of foo is, it just creates a manifest of Any and sends that along like this:

    def fooCopy = foo.copy()(manifest[Any])

    If one has control over the Foo class (e.g. it’s not List) then one workaround it by doing all the copying over in the Foo class by adding a method that will do the proper copying, like localCopy above, and return the result:

    case class Bar[A <: Foo[Any]](foo: A) {
      //def fooCopy = foo.copy()
      def fooCopy = foo.localCopy
    }
    
    val bar = Bar(Foo(1))
    println(bar.fooCopy.myManifest) // Prints "Int"
    

    Another solution is to add Foos type parameter as a manifested type parameter of Bar:

    case class Bar[A <: Foo[B], B : Manifest](foo: A) {
      def fooCopy = foo.copy()
    }
    

    But this scales poorly if class hierarchy is large, (i.e. more members have type parameters, and those classes also have type parameters) since every class would have to have the type parameters of every class below it. It also seems to make the type inference freak out when trying to construct a Bar:

    val bar = Bar(Foo(1)) // Does not compile
    
    val bar = Bar[Foo[Int], Int](Foo(1)) // Compiles
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any overhead using partial classes in case of memory, performance etc? If
Essentially, I am just creating two classes where one class, in this case class
I'm trying to abstract case classes in a module using dependent method types and
I'm attempting to model responses from REST APIs as case classes which I can
I was experimenting with variable constructor arguments for case classes in Scala, but am
I have following use-case: there are several assemblies decorated with ProtoContract classes and I
Lets put the case that we've got two separate classes, each owns a mysqli
Case in point : I've got a handle to a window (for instance, using
I would like to know if it is possible to abstract the copy method
This might be a silly question, but... I've been writing a number of classes

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.