我不确定这是 Groovy 还是 Java 的语法,但见过这样写的赋值语句。
String savedText = ""
...
(savedText) = readText(setting);
包住 savedText 的括号有什么作用,还是只是为了提高可读性?
我不确定这是 Groovy 还是 Java 的语法,但见过这样写的赋值语句。
String savedText = ""
...
(savedText) = readText(setting);
包住 savedText 的括号有什么作用,还是只是为了提高可读性?
I don't know whether this belongs to Groovy or Java, but I've come across assignments written like this:
String savedText = ""
...
(savedText) = readText(setting);
do parentheses enclosing savedText do anything, or are they only meant to improve readability?
Java 和 Groovy 都允许这种“写法”(也就是说语法合法)。Groovy 可以用这种语法进行复合赋值(虽然这里实际上只赋一个值……)
String left, middle, right
(left, middle, right) = ["4", "5", "6"]
不过 Java 没有复合赋值。但 Java Language Specifications 15.26.1 (https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.26.1) 允许在外面加任意多对括号。
当左操作数表达式是字段访问 e.f (§15.11),并且可以被任意多对括号包住时,则
关键只在于完整求值后是否是字段访问(或数组元素访问)。
包住 savedText 的括号有什么作用
没有。
还是只是为了提高可读性?
在我看来,这会降低可读性。不过,风格一致更重要。也许是想让普通赋值和复合赋值的风格保持一致?
String single
(single) = "456"
String left, middle, right
(left, middle, right) = ["4", "5", "6"]
Both Java and Groovy allow this "thing" (that is, the syntax is legal). In Groovy, this syntax supports compound assignment (despite there being just one item here...)
String left, middle, right
(left, middle, right) = ["4", "5", "6"]
Java, however, offers no compound assignment. Still, the Java Language Specifications 15.26.1 (https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.26.1) permits arbitrarily many surrounding parentheses:
When the expression forming the left operand is a field access e.f (§15.11), optionally surrounded by any number of paired parentheses, then:
What counts is whether full evaluation yields an access to a field (or to an array element).
do parentheses enclosing savedText do anything
No.
or are they only meant to improve readability?
To me it reduces readability. A consistent style is more important, however. Perhaps the intention was matching simple assignments to the style of compound assignments?
String single
(single) = "456"
String left, middle, right
(left, middle, right) = ["4", "5", "6"]
Groovy
你展示的语法是一种解构操作。
1.2.1. Multiple assignment (https://groovy-lang.org/semantics.html#_multiple_assignment)
Groovy 支持多重赋值,能让多个变量同时获得各自的值,例如
def (a, b, c) = [10, 20, 'foo'] assert a == 10 && b == 20 && c == 'foo'如果需要,也可以在声明中指定类型。
def (int i, String j) = [10, 'foo'] assert i == 10 && j == 'foo'这不只适用于变量声明,也适用于之前已经声明的变量。
def nums = [1, 3, 5] def a, b, c (a, b, c) = nums assert a == 1 && b == 3 && c == 5数组、列表,以及返回这两种值的方法都可以使用这种语法。
def (_, month, year) = "18th June 2009".split() assert "In $month of $year" == 'In June of 2009'
请记住,即使括号里只有一个要赋值的变量,仍然会发生解构。赋给它的是被解构值的第一个元素。
def (slot) = [3, 6, 7]
assert slot == 3
下面的例子会抛出异常,因为没有可解构的内容
def (slot) = 3
Java
目前这是合法语法,但没有特殊含义
Groovy
The displayed syntax is a kind of destructuring operation.
1.2.1. Multiple assignment (https://groovy-lang.org/semantics.html#_multiple_assignment)
Multiple assignment is available in Groovy, allowing several variables to receive their values together, for instance:
def (a, b, c) = [10, 20, 'foo'] assert a == 10 && b == 20 && c == 'foo'Types may also appear within the declaration, if desired:
def (int i, String j) = [10, 'foo'] assert i == 10 && j == 'foo'This works not only for variable declarations but also for variables declared earlier:
def nums = [1, 3, 5] def a, b, c (a, b, c) = nums assert a == 1 && b == 3 && c == 5Arrays work with this syntax too, along with lists and methods returning either kind:
def (_, month, year) = "18th June 2009".split() assert "In $month of $year" == 'In June of 2009'
Remember that destructuring happens even when the parentheses contain just a single variable for assignment. The assigned result is the initial element of the value being destructured.
def (slot) = [3, 6, 7]
assert slot == 3
This next example raises an exception, as no destructuring can be performed
def (slot) = 3
Java
At present this is legal syntax without any particular meaning
这是我在 GDScript 中使用的枚举声明。 我想随机选择一个枚举变体,而不用列出所有可能的变体。这样以后增加变体时,就不必修改负责随机选择的代码。 我尝试过下面这种方法。 还有这一种。 两种都失败了。一个返回 Null,另一个报告 "Invali…
Nim 中怎样表示求余运算? cell % 7 == 0 会给出 undeclared identifier: '%' 这个错误 Google 搜索和查看 SO 都没找到答案。
怎样才能让函数在执行结束后返回一个值? 例如我有这样的代码。 ROUTINE N ? N ⍴ 12 //它会生成 N 个从 1 到 12 的随机值 (N 是函数的参数)。 我只是想知道怎样拿到函数的结果,然后比如把这个结果传给另一个函数。 谢谢大家的…