Groovy multiple assignment with card

I am having trouble executing a multiple assignment operator for values ​​on a map.

def map = [a:1,b:2] (map.a, map.b) = [3,4] 

this raises an exception:

 expecting ')', found ',' at line: 2, column: 7 

However, this works great:

 def a = 1 def b = 2 (a, b) = [3,4] 
+6
source share
2 answers

Actually, you can do this if you cheat and use .with :

 Map map = [a: 1, b:2] map.with { (a, b) = [3, 4] } assert map.a == 3 assert map.b == 4 
+8
source

He does not support this.

http://groovy.codehaus.org/Multiple+Assignment

 currently only simple variables may be the target of multiple assignment expressions, egif you have a person class with firstname and lastname fields, you can't currently do this: (p.firstname, p.lastname) = "My name".split() 
+4
source

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


All Articles