ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
167 stars 53 forks source link

Function-call nor method-call is supported in compound assignments LHS #343

Open SupunS opened 5 years ago

SupunS commented 5 years ago

Description:

Referring to the following sections of the spec:

compound-assignment-stmt := lvexpr CompoundAssignmentOperator action-or-expr ;

lvexpr :=
   variable-reference-lvexpr
   | field-access-lvexpr
   | member-access-lvexpr

variable-reference-lvexpr := variable-reference
field-access-lvexpr := lvexpr . field-name
member-access-lvexpr := lvexpr [ expression ]

Assume a scenario where I have the following ballerina constructs:

type Node object {
    int index;
    Node parent?;

    public function getParent() return Node {
        return self.parent;
    }
};

public function getRootNode() returns Node {
   ...
}

Scenario 1:

Node n = getRootNode();
n.index += 1;

I can make these two statements inlined:

getRootNode().index += 1;    // not supported by the spec .  ----(1)

Scenario 2:

Node n = ...;
n.getParent().index += 1;    // not supported by the spec .   ----(2)

Of-course both the scenarios can be achieved if I use two-steps. However I don't see any reason to limit the cases (1) and (2), as it complies and consistent to the syntax and the semantics of member-access/method-call expressions in other places.

jclark commented 5 years ago

We decided to follow Java here:

https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-LeftHandSide

We can reconsider at a future date, if it turns out to be a significant problem in practice.