Reputation: 4523
I have a couple questions on applying spread operator on range and map. Refer to code below, error lines are marked.
(1) "assert" works on the updated range, but why doesnt "println" print it?
(2) when we say "*range" groovy can figure out and extend the range. So why doesnt "map" work also, why do we need to say ":map" ?
def range = (1..3)
println range // prints: [1,2,3]
assert [0,1,2,3] == [0,*range] // works ok
println [0, *range] // error
def map = [a:1, b:2]
assert [a:1, b:2, c:3] == [c:3, *:map] // works ok
assert [a:1, b:2, c:3] == [c:3, *map] // error
Upvotes: 0
Views: 511
Reputation: 171114
When you call:
println [0, *range]
it is trying to call getAt
on a property println
. You need to wrap the list in braces to help the parser out:
println( [ 0, *range ] )
And for the second error, *
in this form is the spread operator. It is used to spread Lists.
You have a map, so need to use the spread map operator *:
(as you have seen)
Upvotes: 1