0

I am having a hard time understanding how to get the following code structure to work.

In Scala I have a class MyClass which inherits from SomeClass I added a var member variable in this case called mutableArray and it is being updated in the overridden method overridingSomeClassMethod and is called when I create a new instance of the MyClass a number of times right away. But in main when I try and get the updated mutableArray variable it prints out the instantiated var as if it is immutable or only has scope in the overriding method.

I can't change the method in parent SomeClass, and I tried creating a companion object as well as putting the variable in the encompassing SomeOtherObject but I get the same exact issue.

import scala.collection.mutable.ArrayBuffer

object SomeOtherObject{
  case MyClass(...) extends SomeClass(..){
    var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)

    def overridingSomeClassMethod(...){
      var someReturnVar = 0.0
      mutableArray(0) += 1
      println(mutableArray.mkString) // last output -> 84169
      someReturnVar
    }
  }

  def main(args: Array[String]){
    var mc = new MyClass
    println(mc.mutableArray.mkString) // output -> 00000
  }
}
Peter Neyens
  • 9,770
  • 27
  • 33
h1vpdata
  • 331
  • 2
  • 15

2 Answers2

1

Probably you are hitting the infamous "one question FAQ" about initialization order.

If the method is invoked by the superclass constructor, then your initialization happens after that, resetting the data to zero.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
1

You can use an early initializer:

case MyClass(...) extends {
  var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)
} with SomeClass(..) {
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • I tried this, but because of "def overridingSomeClassMethod" I get the following error "only concrete field definitions allowed in early object initialization section", I assume there aren't any other options I'll need to make a method to run after the sub class is instantiated? – h1vpdata Oct 21 '15 at 22:30
  • 1
    Works for me: http://scastie.org/12697. Note that `def overridingSomeClassMethod` doesn't go in the early initialization section. – Alexey Romanov Oct 21 '15 at 22:34
  • Your code compiles and runs and I am sure I followed it pretty closely, I also upgraded to 2.11.4 (which is the most I can upgrade to without breaking the build) from 2.10.5 and it still didn't work. If this is true (@Alexey if you can confirm), than I think the issue is resolved? – h1vpdata Oct 21 '15 at 23:44
  • In what sense "it still didn't work"? Are you still getting a compilation error or does it compile and not give the desired results? – Alexey Romanov Oct 22 '15 at 03:59
  • Compiles, and runs, but doesn't get the desired result. I see it works in older version of scala http://scastie.org/12719. Outside of parent object being in a different file which I assume makes no difference I am not sure what is wrong with my code. I did make a separate method in MyClass and executed after instantiation and that works. – h1vpdata Oct 22 '15 at 10:14
  • ok, it worked, the issue was an extra call on the overriding method. – h1vpdata Oct 22 '15 at 16:16