0

I am trying to make sense of methods definitions with no arguments and parentheses. In the main method, last one does not work. What is the explanation for that semantically or syntactically?

class X(x:Int){
  def getX1() = x;
  def getX2 = x;
}

object X {
  def main(args: Array[String]) {
    val x = new X(123)
    println(x.getX1)//works
    println(x.getX1())//works
    println(x.getX2) //works
    println(x.getX2())//does not work
  }
}
mert inan
  • 1,537
  • 2
  • 15
  • 26

1 Answers1

0

It's simple. If you have a method defined with () you can choose to call it with or without (). On the other hand, if you have a method defined without (), you can only call it without (). To me, it's a good practice to use () to signify side-effects.

harp seal pup
  • 504
  • 3
  • 4