You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jochen Theodorou (Jira)" <ji...@apache.org> on 2025/01/21 17:03:00 UTC
[jira] [Comment Edited] (GROOVY-11558) Method reference treated as closure in method dispatch
[ https://issues.apache.org/jira/browse/GROOVY-11558?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17915764#comment-17915764 ]
Jochen Theodorou edited comment on GROOVY-11558 at 1/21/25 5:02 PM:
--------------------------------------------------------------------
Method references are using Closure in 2.4.7 as well as in 4.0.24. That did not really change. And I agree with you. Once the method selection finds out the SAM conversion cannot be used here, it should have fallen back to Object. One addition... I tested the code without static type checking. With static type checking and removing the null case it too fails with a runtime error.
was (Author: blackdrag):
Method references are using Closure in 2.4.7 as well as in 4.0.24. That did not really change. And I agree with you. Once the method selection finds out the SAM conversion cannot be used here, it should have fallen back to Object
> Method reference treated as closure in method dispatch
> ------------------------------------------------------
>
> Key: GROOVY-11558
> URL: https://issues.apache.org/jira/browse/GROOVY-11558
> Project: Groovy
> Issue Type: Bug
> Components: Compiler, Static Type Checker
> Affects Versions: 4.0.24
> Reporter: Wouter Peers
> Priority: Major
> Labels: closure, dispatch, overloading
>
> When passing a method reference to an overloaded method of which one of the signatures accepts a functional interface. Groovy seems to treat the method ref as a closure and dispatches to the wrong method signature.
> Example:
> {code:groovy}
> @FunctionalInterface
> interface PresenceAware {
> boolean isPresent()
> }
> class Presence {
> static boolean isPresent(Object o){
> o != null
> }
> static boolean isPresent(PresenceAware p){
> p.isPresent()
> }
> }
> class StringWrapper implements PresenceAware{
> private final String s
> StringWrapper(String s) {
> this.s = s
> }
> @Override
> boolean isPresent() {
> return s!= null && !s.empty
> }
> }
> assert !Presence.isPresent(null)
> assert Presence.isPresent('')
> assert !Presence.isPresent(new StringWrapper(''))
> assert Presence.isPresent(Integer.&compare)
> // => MissingMethodException (in Groovy v4.0.24)
> assert Presence.isPresent(Integer.&valueOf)
> // => MethodSelectionException (in Groovy v4.0.24)
> {code}
> This code runs fine in Groovy v2.4.7—as I would expect, the method references passed in the last two statements are treated as an Object when dispatching to Presence::isPresent mehtod.
> In Groovy v4.0.24 however the method references seem to be (wrongfully) treated as closures and are dispatched to Presence::isPresent(PresenceAware) signature.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)