Multiple Assignment
Groovy supports multiple assignment, i.e. where multiple variables can be assigned at once, e.g.:
You can provide types as part of the declaration if you wish, e.g.:
As well as used when declaring variables (as above) it also applies to existing variables, e.g.:
The syntax works for arrays as well as lists, as well as methods that return either of these, e.g.:
Overflow and Underflow
If the left hand side has too many variables, excess ones are filled with null's, e.g.:
If the right hand side has too many variables, the extra ones are ignored, e.g.:
Notes:
- currently only simple variables may be the target of multiple assignment expressions, e.g. if you have a
personclass withfirstnameandlastnamefields, you can't currently do this:
- currently multiple assignment cannot be used for declaring multiple class fields and initializing them as:
Labels