[
https://issues.apache.org/jira/browse/GROOVY-8788?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17582540#comment-17582540
]
Eric Milles commented on GROOVY-8788:
-------------------------------------
4. overload and override -- I think we all expect {{b.m(new String())}} to
return 3 now and Groovy follow suit
{code:groovy}
class A { }
class B extends A { }
class Cat {
static m(A self, String s) {1}
static m(B self, Object o) {2}
static m(B self, String s) {3}
}
use (Cat) {
def b = new B()
assert b.m(new Object()) == 2
assert b.m(new String()) == 3
}
{code}
5. mixed declaration and extension
{code:groovy}
class A { int m(String s) {4} }
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}
Here we have something interesting and new. Groovy chooses extension method
{{m(String)}} over declared method {{m(String)}}. I think this is how {{String
toString(Object self)}} works. Even in this case for {{b.m(new String())}},
the static compiler selects {{m(Object)}} given two {{m(String)}} options.
Further confirmation for me that SC is improperly weighting the self type B.
> 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)