groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srko─Ź <dinko.sr...@gmail.com>
Subject Re: Syntax question using (identifier 1, identifier 2) on LHS of =
Date Fri, 27 Jan 2017 18:55:59 GMT
On 27 January 2017 at 19:15, Les Hartzman <lhartzman@gmail.com> wrote:
> Hi,
>
> I just saw a reference to using the above syntax, specifically as follows:
>
>    def (string1, string2) = "part1-part2".tokenize("-")
>
> This assigns string1 "part1" and string2 "part2".
>
> So my main question is what is this referred to as?

It's called multiple assignment.

>
> You can't do:
>
>    String (string1, string2) =  "part1-part2".tokenize("-")

Well, you couldn't. What if types were different?

But you could do it like this:

  def (String string1, String string2) = "part1-part2".tokenize("-")

More on multiple assignments here:
http://groovy-lang.org/semantics.html#_multiple_assignment

Cheers,
Dinko

>
> I did find out that you can do:
>
>    String string1
>    String string2
>    (string1, string2) =  "part1-part2".tokenize("-")
>
> Thanks.
>
> Les
>

Mime
View raw message