True or False?
Class C val x1, x2 = new C val y1 @ y2 = new C println (x1 = x2) println (y1 = = y2)
Explain
Val P1, p2 = new C is val p1 = new C; val P2 = shorthand for new C
Val [email protected] = new C is a pattern binder
Explanation
According to the SLS (§4.1), a value definition val p1, ..., pn:t = e is a shorthand for the sequence of value Defini tions val p1:t = e; ...; Val pn:t = e. This means, example the expression new C is evaluated twice and therefore x1 = = x2 is False.
The second value definition is a pattern binder which are defined in sls§8.1.3. A pattern binder [email protected] consists of a pattern variable x and a pattern p. In we case the pattern P was a variable pattern which matches any value and binds the variable name (in we case y2) to th At value. The pattern binder binds that value also to the pattern variable y1. As the same value is bound to both variables Y1 and y2, the expression y1 = = Y2 is true.
Scala Fun Question 15