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

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

Copilot commented on code in PR #2642:
URL: https://github.com/apache/groovy/pull/2642#discussion_r3489775209


##########
src/main/java/groovy/transform/OperatorRename.java:
##########
@@ -152,6 +152,28 @@
      * @return the replacement method name
      */
     String compareTo() default Undefined.STRING;
+    /**
+     * Returns the replacement method name for the {@code in} membership 
operator
+     * (whose default method is {@code isIn}). Defaults to {@link 
Undefined#STRING},
+     * meaning no rename. The receiver is the right operand (the container), so
+     * {@code a in b} dispatches to {@code b.name(a)}. Setting this to {@code 
'isCase'}
+     * restores the pre-GROOVY-9848 value-based membership for maps and char 
sequences
+     * within the annotated scope.
+     *

Review Comment:
   The new `@OperatorRename(isIn=...)` Javadoc suggests setting `isIn='isCase'` 
restores legacy semantics, but it doesn’t mention that `!in` will keep using 
the default behavior unless `isNotIn` is also set. That omission can easily 
lead to inconsistent `in` vs `!in` semantics within the annotated scope.





> Allow membership operator to work on maps
> -----------------------------------------
>
>                 Key: GROOVY-9848
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9848
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Keegan Witt
>            Assignee: Eric Milles
>            Priority: Major
>              Labels: breaking, breaking_change
>
> {code:groovy}def map = [a:1,b:2,z:0].withDefault{3}{code}
> Options:
> # do nothing
>   {code:groovy}
>   assert 'a'  in map
>   assert 'b'  in map
>   assert 'x'  in map // mutates!
>   assert 'z' !in map
>   assert  4  === map.size()
>   {code}
> # change mutation of {{isCase}} -- guard with {{containsKey}}:
>   {code:groovy}
>   assert 'a'  in map
>   assert 'b'  in map
>   assert 'x' !in map
>   assert 'z' !in map
>   assert  3  === map.size()
>   {code}
> # change {{isCase}} to {{containsKey}}:
>   {code:groovy}
>   assert 'a'  in map
>   assert 'b'  in map
>   assert 'x' !in map
>   assert 'z'  in map
>   assert  3  === map.size()
>   // grep and switch behavior change (see below)
>   {code}
> # ask user to test key set
>   {code:groovy}
>   assert 'a'  in map.keySet()
>   assert 'b'  in map.keySet()
>   assert 'x' !in map.keySet()
>   assert 'z' !in map.keySet()
>   assert  3  === map.size()
>   {code}
> # provide {{@OperatorRename}} support:
>   {code:groovy}
>   @OperatorRename(isCase='containsKey')
>   void test() {
>   assert 'a'  in map
>   assert 'b'  in map
>   assert 'x' !in map
>   assert 'z' !in map
>   assert  3  === map.size()
>   }
>   {code}
> # remap operator {{in}} to {{isIn}} (or some such):
>   {code:groovy}
>   // DGMs:
>   boolean isIn(Map map, Object key) {
>     return map != null && map.containsKey(key);
>   }
>   boolean isIn(Object obj, Object value) {
>     return InvokerHelper.invokeMethod(obj, "isCase", new Object[]{value});
>   }
>   assert 'a'  in map
>   assert 'b'  in map
>   assert 'x' !in map
>   assert 'z'  in map
>   assert  3  === map.size()
>   // behavior change for any type that provides "isIn"
>   {code}



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

Reply via email to