groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KARR, DAVID" <dk0...@att.com>
Subject Assigning "leftover" args to trailing array, difference between func params and plain assignment
Date Thu, 13 Aug 2015 05:11:26 GMT
Based on a question I saw on IRC, it appears that the way values are allocated to parameters
differs between two cases, where one case is calling a function, and the other is an "inline"
assignment.

In other words, comparing these two different cases:

	def foo(Object a, Object[] b) { println("a[${a}] b[${b}]") }

And calling it with:

	foo(1, 2, 3, 4)

And the following case:

	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]]

Apparently, the ability to allocate the "rest" of the values to a trailing array parameter
works fine for function parameters, but for the other case (what would you call that?), it
just puts the first value into the array.

Mime
View raw message