groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <>
Subject Re: Assigning "leftover" args to trailing array, difference between func params and plain assignment
Date Thu, 13 Aug 2015 10:44:16 GMT
Am 13.08.2015 07:11, schrieb KARR, DAVID:
> 	def (int a, int[] b) = [1, 2, 3, 4]                   // Given this ...
> 	println("a[$a] b[$b]")
> Putting this all into a script, this outputs this:
> 	a[1] b[[2, 3, 4]]
> 	a[1] b[[2]]

that's multiple assignment we are talking about. It is basically defined 
like this:

for "def (x_0,x_1,...,x_n) = l" we get x_i=l[i] but with type/value 
transformations according to Groovy rules. Or in other words, it is 
equal to:

def x_0 = l[0]
def x_1 = l[1]
def x_n = l[n]

I would have to check in detail, but the access l[i] is not done 
directly, it is done by using an iterator from l.

This means in your case "def (int a, int[] b) = [1, 2, 3, 4]" you 
basically have this:

int a = 1
int[] b = 2

Groovy will then wrap the single value of 2, into an array for this 

As for rules for length. If the iterator does not provide enough 
elements the assignment will fail with a runtime exception. Additional 
elements from the iterator on the other hand are just ignored.

Frankly it is questionable if Groovy should really support that array 
wrapping for "int[] b = 2", especially since something like "int[] b = 
2,3" is not supported. But well... we have it atm.

bye blackdrag

Jochen "blackdrag" Theodorou

View raw message