我在使用map时遇到了一个问题,即如何进行多重赋值语句。
def map = [a:1,b:2]
(map.a, map.b) = [3,4]
这会抛出一个异常:
expecting ')', found ',' at line: 2, column: 7
然而,这个可以正常运行:
def a = 1
def b = 2
(a, b) = [3,4]
我在使用map时遇到了一个问题,即如何进行多重赋值语句。
def map = [a:1,b:2]
(map.a, map.b) = [3,4]
这会抛出一个异常:
expecting ')', found ',' at line: 2, column: 7
然而,这个可以正常运行:
def a = 1
def b = 2
(a, b) = [3,4]
实际上,如果您使用.with
进行欺骗,那么您可以做到这一点:
Map map = [a: 1, b:2]
map.with {
(a, b) = [3, 4]
}
assert map.a == 3
assert map.b == 4
currently only simple variables may be the target of multiple assignment expressions, e.g.if you have a person class with firstname and lastname fields, you can't currently do this:
(p.firstname, p.lastname) = "My name".split()