ScalaDoc - Example for Method that has side effect without parentheses


#1

Below piece of writing from scaladoc for parameter less methods:

//
To summarize, it is encouraged style in Scala to define methods that take no
parameters and have no side effects as parameterless methods, i.e., leaving
off the empty parentheses. On the other hand, you should never define a
method that has side-effects without parentheses, because then invocations
of that method would look like a field selection. So your clients might be
surprised to see the side effects. Similarly, whenever you invoke a function
that has side effects, be sure to include the empty parentheses when you
write the invocation.

//

Question: When a method without parentheses which has side-effects is invoked ,how does it look like a field selection and client will see the side effects ? can give an example if any…


#2
class ListBuffer[A](as: A*) {
  private var list = as.toList
  def length = {
    if (list.isEmpty) 0 
    else {
      val size = list.size
      list = list.tail
      size
    }
  }
}

val buffer = new ListBuffer(1, 2, 3)
println(buffer.length) // 3
println(buffer.length) // 2
println(buffer.length) // 1
println(buffer.length) // 0

I would be surprised when I inspect the length property of a list that the list also gets shorter.