Constructor Overriding


#1

Hi, I want to have a class which gets for its constructor a mutable variable.
How can I define the constructor to clone it?
I tried overriding the constructor, but it’s not possible. Is there a way to override the constructor and any elegant way to write the class definition so it will clone the variable.


#2

Can you provide an example of what you’re trying to do? Maybe we can help you get it to compile.


#3

I’m just experimenting with the language. I’m new to the language. The thing is that it is quite common for a programmer to want that a mutable-typed variable to be copied, when sent to create a new object, because you don’t want it to be changed from outside to the class. That’s actually bypassing the whole idea of OOP, to seperate everything into individual classes with diffrenet responsibilities. So, it’s weird that there’s no obvious solution in the language.
I can simply solve this by having an empty constructor (not defining any class variables in the class signature), define all the fields inside the class, and then overload the “this” method, but that a Java solution, not very suitable for Scala code.


#4

Hello,

Note: cloning is rarely done in either Java or Scala. I don’t remember whether I ever did it.

Do you mean like this?

**scala> class MutableCloneableThing(var stuff: String) extends Cloneable { override def clone(): MutableCloneableThing = new MutableCloneableThing(stuff) }
defined class MutableCloneableThing

class Cloner(mct: MutableCloneableThing) { val myMct: MutableCloneableThing = mct.clone }
defined class Cloner

val mct0 = new MutableCloneableThing(“Hello”)
mct0: MutableCloneableThing = MutableCloneableThing@282aea3c

val cloner = new Cloner(mct0)
cloner: Cloner = Cloner@7ab2e018

mct0.stuff = “yo”
mct0.stuff: String = yo

mct0
res0: MutableCloneableThing = MutableCloneableThing@282aea3c

mct0.stuff
res1: String = yo

cloner.myMct.stuff
res2: String = Hello**

Best, Oliver


#5

It’s worth noting that mutables, while allowed in Scala, are a good deal less common in idiomatic Scala than in, say, typical Java. It’s pretty common for even not-all-that-FP Scala code to be written with few or no vars. So the language isn’t heavily optimized around them.

And as @curoli says, cloning is also pretty uncommon. You see .copy() all over the place, but that’s not true (deep) cloning – as defined in case classes, that’s a shallow copy, usually passing most of the members intact.


#6

Calling the actual .clone() method isn’t common in Java code, but making
defensive copies of shared mutable data in multithreaded code is.
Several points in /Effective Java/ (a book that catalogs
commonly-accepted “good” practices) address this.

To the OP, consider an apply method on a companion object:

class Foo(private val m: MutableThing) { ... }

object Foo {
  def apply(m: MutableThing): Foo = {
    val copy = defensiveCopyOf(m)

    new Foo(copy)
  }
}

But before that, consider if you really need something mutable, and if
you would be better off with something immutable instead.