openlang

Why put parentheses around a variable on the left of an assignment?

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?

The conversation continues 2

Follow the reasoning
  1. Permalink

    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"]
  2. Permalink

    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 == 5

    Arrays 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

Other sides of the same question

  1. Choosing enum entries randomly with GDScript in Godot 3.3 without listing each one?

    Here's the enum declaration I'm using in GDScript: I'd like to choose an enum variant randomly without listing every possible…

    Replies 2Latest activity gdscript
  2. Nim remainder syntax

    How is modulus expressed in Nim? cell % 7 == 0 gives undeclared identifier: '%' Neither a Google search nor looking through S…

    Replies 2Latest activity nim
  3. Returning results from an APL function

    How do I make my function return something once execution has finished? For instance, I have: ROUTINE N ? N ⍴ 12 //That gener…

    Replies 2Latest activity apl