nktsg
nktsg

Reputation: 143

What kind of syntax is this in Groovy?

(postUrl, config, isLoggedIn) = getSpringSecurityLoginConfig()

I couldn't find in the official documentation what kind of syntax is this?

Upvotes: 1

Views: 68

Answers (1)

tim_yates
tim_yates

Reputation: 171154

That's multiple assignment.

There's a blog post here, and documentation here:

Groovy supports multiple assignment, i.e. where multiple variables can be assigned at once, e.g.:

def (a, b, c) = [10, 20, 'foo']
assert a == 10 && b == 20 && c == 'foo'

You can provide types as part of the declaration if you wish:

def (int i, String j) = [10, 'foo']
assert i == 10 && j == 'foo'

As well as used when declaring variables it also applies to existing variables:

def nums = [1, 3, 5]
def a, b, c
(a, b, c) = nums
assert a == 1 && b == 3 && c == 5

The syntax works for arrays as well as lists, as well as methods that return either of these:

def (_, month, year) = "18th June 2009".split()
assert "In $month of $year" == 'In June of 2009'

Upvotes: 5

Related Questions