Asterisks before array names in Groovy?

I'm a little new to Groovy, so I'm sure this is one of those extremely obvious things ... but it's hard to find through Google.

In other languages, stars tend to represent pointers. However, in this piece of Groovy code:

byte[] combineArrays(foo, bar, int start) { [*foo[0..<start], *bar, *foo[start..<foo.size()]] } 

I can only imagine that this is not so. I mean pointers? Groovy?

I assume that this code intends to pass members from foo and bar as opposed to a multidimensional array. So what exactly do asterisks mean?

Many thanks for your help.

+6
source share
1 answer

When used in this way, the * operator extends List or Array to the argument list. That didn't help, did it? What about an example? Let's say we have this function:

 def add(Number a, Number b) { return a + b } 

And this list

 def args = [1, 2] 

We should not do this:

 add(args) 

because the function expects two numeric arguments. But we can do this:

 add(*args) 

because the * operator converts a list of 2 elements into 2 arguments. You can use this operator with lists and arrays.

+10
source

Source: https://habr.com/ru/post/905528/


All Articles