You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2025/01/25 15:46:00 UTC
[jira] [Assigned] (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:all-tabpanel ]
Eric Milles reassigned GROOVY-11558:
------------------------------------
Assignee: Eric Milles
> 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
> Assignee: Eric Milles
> 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)