Details
-
Type:
New Feature
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: Compiler
-
Labels:None
-
Number of attachments :
Description
I'd like to propose 'method apply and assign' operator.
purpose
We already have 'apply and assign' operators like:
*= += -= != :
By using those operators, we can make shorten expression:
x.y.z = x.y.z * a
to:
x.y.z *= a
On the other hand, we don't have the same way for method calls. This proposal introduces a shorthand way of apply and assign the result to itself for method calls in Groovy.
Proposal
Introduce new operators '=.' or '=.' like
a =. method(x)
or
a .= method(x)
means:
a = a .method(x)
Example
s = "ABC" s.= replaceAll('A', 'X') // same as s = s.replaceAll('A', 'X') assert s == 'XBC'
s = "ABC" s.= padLeft(5) // same as s = s.padLeft(5) assert s == ' ABC'
Comparison of .= and =.
'.=' is along with the other assign operators.
For example, based on this rule:
a = a X <b> a X= <b>
.= is interpreted in following way:
a = a.<xxx()> a .= <xxx()>
=. is based on a rule which implies under elvis operator.
For example,
a ? a : b
is shorten by using elvis
a ? : b
So,
a = a.xxx()
is shorten to
a =. xxx()
IMHO, .= is better because it is easy to remind and has similarity of other assign operators.