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
  }
}

2 Answers 2

1

You can use an early initializer:

case MyClass(...) extends {
  var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)
} with SomeClass(..) {
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Works for me: scastie.org/12697. Note that def overridingSomeClassMethod doesn't go in the early initialization section.
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?
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?
Compiles, and runs, but doesn't get the desired result. I see it works in older version of scala 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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.