[ 
https://issues.apache.org/jira/browse/GROOVY-11970?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18077005#comment-18077005
 ] 

ASF GitHub Bot commented on GROOVY-11970:
-----------------------------------------

blackdrag commented on code in PR #2499:
URL: https://github.com/apache/groovy/pull/2499#discussion_r3159829110


##########
src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java:
##########
@@ -998,4 +998,34 @@ public static Object bitwiseNegate(final Object value) 
throws Throwable {
             throw unwrap(gre);
         }
     }
+
+    /**
+     * GEP-15: dispatcher for compound-assignment operators in dynamic Groovy.
+     * If {@code receiver} responds to {@code assignName} with the supplied 
argument,
+     * invoke it and return {@code receiver} (so the caller's STORE assigns 
the same
+     * reference back, leaving the in-place mutation visible). Otherwise fall 
back to
+     * {@code baseName} and return its result for the caller to assign.
+     */
+    public static Object compoundAssign(final Object receiver, final Object 
arg,
+                                        final String assignName, final String 
baseName) throws Throwable {
+        if (receiver != null) {
+            MetaClass mc = InvokerHelper.getMetaClass(receiver);
+            if (!mc.respondsTo(receiver, assignName, new 
Object[]{arg}).isEmpty()) {
+                mc.invokeMethod(receiver, assignName, arg);
+                return receiver;
+            }
+        }
+        return InvokerHelper.invokeMethod(receiver, baseName, arg);

Review Comment:
   SCB already has methods for invocation, why not use those?



##########
src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java:
##########
@@ -998,4 +998,34 @@ public static Object bitwiseNegate(final Object value) 
throws Throwable {
             throw unwrap(gre);
         }
     }
+
+    /**
+     * GEP-15: dispatcher for compound-assignment operators in dynamic Groovy.
+     * If {@code receiver} responds to {@code assignName} with the supplied 
argument,
+     * invoke it and return {@code receiver} (so the caller's STORE assigns 
the same
+     * reference back, leaving the in-place mutation visible). Otherwise fall 
back to
+     * {@code baseName} and return its result for the caller to assign.
+     */
+    public static Object compoundAssign(final Object receiver, final Object 
arg,
+                                        final String assignName, final String 
baseName) throws Throwable {
+        if (receiver != null) {
+            MetaClass mc = InvokerHelper.getMetaClass(receiver);
+            if (!mc.respondsTo(receiver, assignName, new 
Object[]{arg}).isEmpty()) {
+                mc.invokeMethod(receiver, assignName, arg);
+                return receiver;
+            }
+        }
+        return InvokerHelper.invokeMethod(receiver, baseName, arg);
+    }
+
+    /**
+     * GEP-15: helper for {@code @OperatorRename(plusAssign="...")} expression 
rewrites.
+     * The user's renamed name is authoritative (no fallback), and the 
expression value of
+     * {@code x op= y} is the (mutated) {@code x}, not the called method's 
return value.
+     */
+    public static Object invokeRenamedCompoundAssign(final Object receiver, 
final Object arg,
+                                                     final String name) throws 
Throwable {
+        InvokerHelper.invokeMethod(receiver, name, arg);
+        return receiver;

Review Comment:
   why not use a normal method call for this then? I don't see why we need an 
extra method in SCB for this





> Provide support for compound assignment operator overloading (GEP-15)
> ---------------------------------------------------------------------
>
>                 Key: GROOVY-11970
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11970
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to