이게 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에서 enum을 이렇게 선언했어요: 가능한 모든 변형을 나열하지 않고 enum 변형을 무작위로 고르고 싶어요. 나중에 변형을 더 추가해도 무작위로 선택하는 코드를 수정하지 않도록요. 이런 방법을 시도해 봤어요:…
Nim에서는 나머지를 어떻게 표현하나요? cell % 7 == 0를 쓰면 undeclared identifier: '%'가 나옵니다 Google로 검색해도 SO를 찾아봐도 답을 찾지 못했습니다.
함수 실행이 끝나면 어떤 값을 반환하게 하려면 어떻게 하나요? 예를 들어 이런 것이 있습니다: ROUTINE N ? N ⍴ 12 //이것은 1부터 12까지의 난수 N개를 만듭니다 (N은 함수의 인수입니다). 함수의 결과를 돌…