Search code examples
scalafunctionvar

how to call a private function in object in scala


this is for the current score in the class:

private var score: Int = 0

def initialiseTest2(): Game = {
  return new Game(List((3,3),(3,4),(3,5),(5,3),(5,4),(5,5)), List((4,4,(x: Int) => x+1), (6,3, if(score == 0){ (x: Int) => x+1 }else { (x: Int) => x+3 })), 3,2)
}

def initialiseTest2 is in object and the part where there is the if(score == 0) that part I need to call the private var score how can I do that, when I just type score it gives an error saying not found: value score


Solution

  • Create a getter function to return the copy of score. Then, you can access the current score from the tests.

    object Score {
    
     private var score: Int = 0
    
     def currentScore(): Int = score
    
    }