I found when assigning to multi variables, the () is important. please see:
scala> mytest
val res0: (Int, Int, Int) = (1,2,3)
scala> val x,y,z = mytest
val x: (Int, Int, Int) = (1,2,3)
val y: (Int, Int, Int) = (1,2,3)
val z: (Int, Int, Int) = (1,2,3)
scala> val (x,y,z) = mytest
val x: Int = 1
val y: Int = 2
val z: Int = 3
I almost mentioned that the “multi-definition” has nothing to do with patterns.
The other form takes a pattern, just like the case in a pattern match.
Currently, Scala 2 lints if it could fail, and Scala 3 will restrict it:
âžś ~ scala -Xlint
Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.
scala> val (x,y,z) = (1,2,3): Any
^
warning: match may not be exhaustive.
It would fail on the following input: (x: Any forSome x not in (?, ?, ?))
val x: Any = 1
val y: Any = 2
val z: Any = 3
scala> (1,2,3) match { case (x,y,z) => x+y+z }
val res1: Int = 6
Welcome to Scala 2.13.9-20220131-161511-c4b0f17 (OpenJDK 64-Bit Server VM, Java 17.0.1).
Type in expressions for evaluation. Or try :help.
scala> for (case x <- List(42)) yield x
val res0: List[Int] = List(42)
scala> val case (x, y, z) = (42, 27, 3)
^
error: illegal start of simple pattern
Note that I proposed a syntax tweak once that was rejected because it did not apply to all patterns. I think this example is an obvious shortcoming, back me up on this.
Yeah, I get the expression is evaluated every time. I just thought Value was a macro.
But I just looked at the source, and is just a method that calls another method chain that ends up calling the constructor of some protected class that mutates some variables inside the extending class… at some point I would rather prefer a macro
Before I was waylaid by the case question, I was going to say that not only is Enumeration not a macro, it is an early example of what you can do in a library without an enum keyword. It preceded the macro experiment. The internals are famously gnarly.
The result of a 15-year research project is that they concluded you need an enum keyword.
At least a middle-aged master’s student somewhere finally got his diploma.
Oh, you can still search “Enumeration must DIE” where Martin says, “Enumeration comes with a usage pattern. If you use it wrongly, it breaks.” I was just saying “divergent implicit expansion” spells “die”, and then everyone piled on. Then Eugene “Burmakro” Burmako added, “I think we can make Enumeration a trait macro, given we empower trait macros to read contents of host classes.” Those were heady days of innovation.