Scala’s handling of superclass constructor parameters is confusing me…
with this code:
class ArrayElement(val contents: Array[String]) {
...
}
class LineElement(s: String) extends ArrayElement(Array(s)) {
...
}
LineElement is declared to extend ArrayElement, it seems strange to me that the Array(s) parameter in ArrayElement(Array(s)) is creating an Array instance – runtime??? Is this scala’s syntatic sugar or is there something else going on here?
Yes, the
Array(s)expression is evaluated at run-time.Scala allows expressions in the calls to a superclass’ constructor (similar to what Java does with its use of
super(...)). These expressions are evaluated at run-time.