Skip to content

Instantly share code, notes, and snippets.

@baybatu
Last active July 24, 2016 23:52
Show Gist options
  • Save baybatu/9fc78575dd85c5b0a4828f6f9ed6c378 to your computer and use it in GitHub Desktop.
Save baybatu/9fc78575dd85c5b0a4828f6f9ed6c378 to your computer and use it in GitHub Desktop.
Using value of a variable as map key in Groovy

If you want to use a variable value as key of your map, then you should wrap your key with parenthesis. It makes key to be considered as expression instead of String literal.

String keyVar = "mykey"

def map0 = [keyVar : 12]
assert map0.keyVar == 12
assert !map0.mykey

def map1 = [(keyVar) : 12]
assert map1.mykey == 12
assert !map1.keyVar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment