Discussion:
[scala-user] Copy constructor?
Eric Fowler
2015-04-29 04:19:17 UTC
Permalink
Here is a super easy one for the panel: How do I create a duplicate of an
object?

scala> class C(val c:Int, var s:String)
defined class C

scala> val c = new C(1, "Hello")
c: C = ***@5d9b7a8a

scala> println(c.s)
Hello

scala> val b = c
b: C = ***@5d9b7a8a

scala> b.s = "Goodbye"
b.s: String = Goodbye

scala> println(c.s)
Goodbye


I want the last line to print 'Hello', not goobye: a memberwise copy into a
new object is desired.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason Zaugg
2015-04-29 04:29:41 UTC
Permalink
Post by Eric Fowler
Here is a super easy one for the panel: How do I create a duplicate of an
object?
scala> class C(val c:Int, var s:String)
defined class C
scala> val c = new C(1, "Hello")
scala> println(c.s)
Hello
scala> val b = c
scala> b.s = "Goodbye"
b.s: String = Goodbye
scala> println(c.s)
Goodbye
I want the last line to print 'Hello', not goobye: a memberwise copy into
a new object is desired.
The closest thing in Scala is the copy method that the compiler adds to case
classes <http://docs.scala-lang.org/tutorials/tour/case-classes.html>.

scala> case class C(val c:Int, val s:String)
defined class C
scala> c.copy(s = "Goodbye")
res2: C = C(1,Goodbye)

scala> c.copy(c = 2)
res3: C = C(2,Hello)

scala> c.copy()
res4: C = C(1,Hello)

​
-jason
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oliver Ruebenacker
2015-04-29 12:24:47 UTC
Permalink
Hello,

Override the java.lang.Object.clone
<http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()> method
with a public method and make it extend java.lang.Cloneable
<http://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html> to
indicate that the clone method can be used (it throws an exception
otherwise).

The reason there is no automatic implementation of cloning is that a
human being needs to decide which fields should be shared with the clone
and which ones need to be cloned, too. The copy method of Scala's case
classes makes a copy that shares all fields (shallow copy). Shallow copies
are fine if all fields are immutable, which is how case classes are usually
used. Your example, however, indicates you don't want a shallow copy.

Making a complete deep copy (clone fields, and fields of fields, etc) is
often not desirable (e.g. unnecessary for immutable objects) and sometimes
it is impossible because some objects are not meant to be cloned (e.g. a
Thread or Runtime object) and a naive deep copy would trip over circular
compositions (e.g. on object is a field of one of its own fields).

So, it is up to you what exactly the clone method should do.

Best, Oliver
Post by Jason Zaugg
Post by Eric Fowler
Here is a super easy one for the panel: How do I create a duplicate of an
object?
scala> class C(val c:Int, var s:String)
defined class C
scala> val c = new C(1, "Hello")
scala> println(c.s)
Hello
scala> val b = c
scala> b.s = "Goodbye"
b.s: String = Goodbye
scala> println(c.s)
Goodbye
I want the last line to print 'Hello', not goobye: a memberwise copy into
a new object is desired.
The closest thing in Scala is the copy method that the compiler adds to case
classes <http://docs.scala-lang.org/tutorials/tour/case-classes.html>.
scala> case class C(val c:Int, val s:String)
defined class C
scala> c.copy(s = "Goodbye")
res2: C = C(1,Goodbye)
scala> c.copy(c = 2)
res3: C = C(2,Hello)
scala> c.copy()
res4: C = C(1,Hello)
​
-jason
--
You received this message because you are subscribed to the Google Groups
"scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Oliver Ruebenacker
Solutions Architect at Altisource Labs <http://www.altisourcelabs.com/>
Be always grateful, but never satisfied.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...