views:

272

answers:

4

I would like to know if it is possible to abstract the copy method of case classes. Basically I have something like sealed trait Op and then something like case class Push(value: Int) extends Op and case class Pop() extends Op.

The first problem: A case class without arguments/members does not define a copy method. You can try this in the REPL.

scala> case class Foo()
defined class Foo

scala> Foo().copy()
<console>:8: error: value copy is not a member of Foo
       Foo().copy()
             ^

scala> case class Foo(x: Int)
defined class Foo

scala> Foo(0).copy()
res1: Foo = Foo(0)

Is there a reason why the compiler makes this exception? I think it is rather unituitive and I would expect every case class to define a copy method.

The second problem: I have a method def ops: List[Op] and I would like to copy all ops like ops map { _.copy() }. How would I define the copy method in the Op trait? I get a "too many arguments" error if I say def copy(): Op. However, since all copy() methods have only optional arguments: why is this incorrect? And, how do I do that correct? By making another method named def clone(): Op and write everywhere def clone() = copy() for all the case classes? I hope not.

+1  A: 
  1. What would be the benefit of a compiler generated copy method for case classes without any arguments? This would just return a new Foo, and not copy anything.
  2. To quote Lukas Rytz (I believe he implemented it):
The copy methods are only generated if there is no member named"copy" in the class, directly defined or inherited.
Mirko Stocker
The benifit would be obviously consistency.
Joa Ebert
Ok, I agree. But it wouldn't have many benefits because you can't really abstract over the copy method.
Mirko Stocker
+1  A: 

Why do you need to create identical copies of your case class instances? Case classes are, by default, immutable so can be safely shared.

In any case, I don't think you can do what you're asking with default parameters:

scala> trait Op { def copy():Op }          
defined trait Op

scala> case class Op1(v:Int) extends Op    
<console>:6: error: class Op1 needs to be abstract, since method copy in trait Op of type ()Op is not defined
       case class Op1(v:Int) extends Op

The compiler doesn't create methods with all combinations of the optional parameters in the defining class. The default values are inserted in the place where the method is called.

Ben Lings
Because I need to reference the created objects by instance and not by value. It does not have to do anything with sharing or a mutable/immutable discussion. By the way: you posted a question, not an answer.
Joa Ebert
If you don't need value semantics, why are you using a case class?
Ben Lings
Pattern matching for instance. Basically I like all of their features but I have to have by-reference equality and by-value equality as well. As misto already pointed out, the compiler won't generate a copy() method at all in the example.
Joa Ebert
A: 

Upvoted Ben's answer. But what if you wanted to something like this:

sealed trait Op 
case class Push(value: Int, context:String) extends Op
case class Pop(context:String) extends Op

val stackOps = List(Push(3, "foo"), Pop("foo"))

def copyToContext(newContext:String, ops:List[Op]): List[Op] = {
    // ... ?
}

val changedOps = copyToContext("bar", stackOps)

// would return: List(Push(3, "bar"), Pop("bar"))
huynhjl
+1  A: 

You seem to be confusing copy with clone. The goal of copy is to make an almost identical copy, but with something changed. What that something might be depends on the parameters of the case class, so it's not possible to make it a common method.

In the case of case class X(), it doesn't make much sense to have a copy method, as there's nothing there to be changed.

On the other hand, clone is a Java method whose goal is to produce perfect copies of an object, which seems to be what you want.

Daniel
Properly used (having no mutable state), case classes with no properties make no sense, either, since every instance is indistinguishable from every other instance.
Randall Schulz
I do not agree. clone is not generated for case classes but copy is. And clone is not accessable for case classes by default. My intent with this question was to get around a copy and paste massacre. Speed is important so relfections are out of the game. The result? I wrote "override def clone() = copy()" and "override def clone() = CaseClass()" in 150+ case classes. IMHO this is far from optimal and could have been done much easier.
Joa Ebert
@Joa But if a case class has no parameters, each object in it is equal to all others. Except for internal vars and vals, which are not copied by `copy` anyway.
Daniel