I am confused when I first come across the following piece of code.
In the class Element, there are three function definitions.
Why height and width can use contents directly as a variable of Array[String]?
Is it because every function in Scala is an object or some other rule?
I come from C++ world, so the definition really puzzles me.
abstract class Element {
def contents: Array[String]
def height: Int = contents.length
def width: Int = if (height == 0) 0 else contents(0).length
}
Elementclass as written. There are three methods. A (nearly) equivalent function for thecontentsmethod would be declared asval contents: () => Array[String]. See Difference between method and function in Scala and the blog posts linked from that question to understand why this distinction matters.