Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Note
titleSuperceded

Note that an initial implementation of multiple assignment is already built in to Groovy 1.6 and above. This document is kept for historical purposes and because it contains suggestions for advanced features not yet implemented. See Multiple Assignment for more details of the current implementation.

Introduction

This page looks at proposed changes to the Groovy language to support multiple assignment. Such support would allow for code such as:

Code Block
a, b = b, a    // will transparently handle any temporary storage

list = [1, 2, 3]
def (c, d, e) = list
assert c == 1 && d == 2 && e == 3

It is meant to be a discussion document, not a formal definition, we will work out the impact on the grammar etc. once we clarify our intention.

Single Assignment

Some existing declarations involving single assigment:

Code Block
def a = 'a' // for examples below
def b = 'b' // for examples below
def c = 'c' // for examples below

//------ Existing: Java style, simple case (Proposal leaves unchanged)
def x1,y1,z1 = a  // only z1 is equal to a, the others are undefined

assert x1 == null
assert y1 == null
assert z1 == a

//------ Existing: Java style, list case (Proposal leaves unchanged)
def x2, y2, z2 = [a,b,c] // only z2 is equal to [a,b,c], the others are undefined

assert x2 == null
assert y2 == null
assert z2 == [a,b,c]

//------- Existing: Java style, multiple equals case (Proposal leaves unchanged)
def x3 = a, y3 = b, z3 = c // obvious
assert x6 == a
assert y3 == b
assert z3 == c

//------- Existing: Java Java style, multiple lists case (Proposal leaves unchanged)
def x4 = [a,b], y4 = [b,c], z4 = [a,c] // obvious
assert x4 == [a,b]
assert y4 == [b,c]
assert z4 == [a,c]

Variable declarations

In order to not conflict with existing definitions, any variable declaration which wishes to make use of multiple assignments must surround the 'tuple' of variable declarations in round brackets. Whenever such a round bracket is found, the compiler will match the tuple with a list. The list will be expected to be where it would be in the case of single assignment. The compiler will 'unpack' the list into the tuple of variables.

Code Block
def (x5,y5,z5) = [a,b,c]       // common type goes before round brackets
def (x6,y6) = [a,b], z6 = c    // x,y,z/5,6,7 must all be undefined
def list = [b, c]
def x7 = a, (y7, z7) = list    // list can be explicit or implicit

assert x5 == a && x6 == a && x7 == a
assert y5 == b && y6 == b && y7 == b
assert z5 == c && z6 == c && z7 == c

Nested variations are also supported (this may be deferred initially):

Code Block
def ((x8,y8),z8) = [[a,b],c]
assert x8 == a && y8 == b && z8 == c

Differing sized tuples and lists are catered for by ignoring extra list members and leaving any untargeted variables in the tuple set to null:

Code Block
def (x9,y9,z9) = [a,b]
assert x9 == a && y9 == b && z9 == null

def (x10,y10,z10) = [a,b,c,d]  // d unused
assert x10 == a && y10 == b && z10 == c

There can be at most one [at the end like varargs?] 'tuple spread' operator which takes the rest of the list:

Code Block
def (first11,*rest11) = [a,b,c,d]

assert first11 == a
assert rest11 == [b, c, d]

Setting a bunch of variables to the same (or related) values is accomplished using normal list conventions:

Code Block
def (x12,y12,z12) = [a] * 3
assert [x12, y12, z12] == [a, a, a]

def (x13,y13,z13) = 0..2
assert [x13, y13, z13] == [0, 1, 2]

def string = 'abcdefghi'
def (x14,y14,z14) = (1..3).collect{ string[0..it*2]}
assert [x14, y14, z14] = ["abc", "abcde", "abcdefg"]

The tuple list is strictly a list (apart from the nesting seen earlier) and may not contain assignments itself:

Code Block
// INVALID
def (x15 = c, y15, z15) = [a,b,c]

Example involving nulls:

Code Block
def (x16,y16,z16)
assert x16 == null
assert y16 == null
assert z16 == null

Examples involving real world use:

Code Block
def (name, address1, address2, postcode) = myData.tokenize("\n").toList()
// acts like first/head/top
def (name) = myData.tokenize("\n").toList()
// equivalent to above but 'self documenting'
def (name, *ignored) = myData.tokenize("\n").toList()

Use in Expressions

The left hand side (LHS) of an expression involving the simple assignment operator, =, can be replaced with a tuple. In this case, the variables will be predefined or binding variables and the result of the RHS of the expression will be a (possibly coerced) list which will be unpacked into the tuple.

Code Block
def list = [a, b]
def x17, y17
(x17, y17) = list
assert [x17, y17] == [a, b]

Where it is not ambiguous, the round brackets can be removed from the tuple list when used in such expressions, so the above could be written as:

Code Block
x17, y17 = list    // LHS turned into (x17, y17)

If the right hand side after any assignment operator contains a comma separated list of subexpressions, they will be assumed to make up a list (i.e. automatically turned into a list), so the above could be written as:

Code Block
x17, y17 = a, b    // RHS turned into [a, b]

[Note: the above probably requires lots of pouring over the grammar!]

If you find you need a tuple with different typed items inside, use an expression form rather than the variable declaration form which requires the types to be the same, e.g.:

Code Block
def myMonth(){ [2000, "Jan", 1] }
String month
int day, year
(year, month, day) = myMonth()

Unlike with declarations, the LHS doesn't need to simply be a variable:

Code Block
def map = [a:1, b:2]
(map.a, map.b) = [3, 4]
assert map == [a:3, b:4]

a = 10..15
i = 3
i, a[i] = i+1, 20
assert i == 4 && a[3] == 20 && a[4] == 14 

Other examples which need to work:

Code Block
x = 0
a, b, c = x, (x += 1), (x += 1)
assert [a, b, c] == [0, 1, 2]

year, month, day = "2007-03-20".split('-')

Other assignment operators

These may not be implemented initially, but we attempt to define them here. It includes these:

Code Block
*= /= %= += -= <<= >>= >>>= &= ^= |=

First, a tuple on the LHS of a special assignment operator:

Code Block
a, b *= 3
// form list [a, b] (tuple spread operator ignored here if present)
// call multiply() operator with arg 3
// unpack result into (a, b) tuple (spread operator applies to unpacking)

// example
def x18 = a, y18 = b
x18, *y18 *= 3
assert x18 = a
assert y18 = [b, a, b, a, b]

Now, an example with special treatment on the RHS:

Code Block
yourObject *= 1, 2
// calls the multiply() method on yourObject with the list [1, 2]

As an alternative to the semantics described here, we could define 'a, b *= 3' to be the same as 'a *= 3; b *= 3'. This seems more useful but then if the semantics were to be consistent, then 'a, b = 3' should mean 'a = 3; b = 3'.

Use in Method declarations

No change, no grouping of method parameters is supported, i.e. the following is invalid:

Code Block
// INVALID
def myMethod((a, b), c) { ... }

See Also

http://jira.codehaus.org/browse/GROOVY-158