[
https://issues.apache.org/jira/browse/GROOVY-8788?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17582532#comment-17582532
]
Eric Milles commented on GROOVY-8788:
-------------------------------------
Let me see if I can break this down from your examples, with some small edits
of mine.
1. overload is more specific -- nothing surprising here
{code:groovy}
class A {
def m(Object o) {1}
}
class B extends A {
def m(String s) {2}
}
def a = new A()
assert a.m(new Object()) == 1
assert a.m(new String()) == 1
def b = new B()
assert b.m(new Object()) == 1
assert b.m(new String()) == 2
{code}
2. overload is more general -- selection is based on argument type
{code:groovy}
import static groovy.test.GroovyAssert.shouldFail
class A {
def m(String s) {1}
}
class B extends A {
def m(Object o) {2}
}
def a = new A()
shouldFail(MissingMethodException) { a.m(new Object()) }
assert a.m(new String()) == 1
def b = new B()
assert b.m(new Object()) == 2
assert b.m(new String()) == 1
{code}
3. category/extension overloads -- same behavior as declared methods; OP
expects this behavior
{code:groovy}
class A { }
class B extends A { }
class Cat {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
use (Cat) {
def a = new A()
assert a.m(new String()) == 1
def b = new B()
assert b.m(new Object()) == 2
assert b.m(new String()) == 1
}
{code}
Hopefully nothing surprising so far. #1 and #2 run the same under static
compilation. #3 requires a little more work since {{use}} cannot be used under
static compilation.
> Inconsistency in extension method selection with @CompileStatic
> ---------------------------------------------------------------
>
> Key: GROOVY-8788
> URL: https://issues.apache.org/jira/browse/GROOVY-8788
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation, Static Type Checker
> Affects Versions: 2.4.15, 2.5.2
> Reporter: Daniil Ovchinnikov
> Assignee: Eric Milles
> Priority: Major
> Labels: breaking
>
> Given properly registered extension class:
> {code:java|title=MyExtensions.java}
> public class MyExtensions {
> public static void foo(Object self, String s) {
> System.out.println("Object#foo(String)");
> }
> public static void foo(String self, Object o) {
> System.out.println("String#foo(Object)");
> }
> }
> {code}
> Run
> {code:java|title=playground.groovy}
> void usageExt() {
> "".foo("") // prints "Object#foo(String)" which is correct
> }
> @groovy.transform.CompileStatic
> void usageExtStatic() {
> "".foo("") // prints "String#foo(Object)" which is questionable
> }
> usageExt()
> usageExtStatic()
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)