[
https://issues.apache.org/jira/browse/GROOVY-8788?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17581874#comment-17581874
]
Jochen Theodorou edited comment on GROOVY-8788 at 8/19/22 2:04 PM:
-------------------------------------------------------------------
>From the comments I am not sure the fix goes in the right direction - of
>course I could be totally wrong here.
{code:java}
class A {
def m(o){1}
}
class B extends A {
def m(String s) {2}
}
def a = new A()
assert a.m(new Object()) == 1
def b = new B()
assert b.m(new Object()) == 1
assert b.m("") == 2
{code}
in this case it is obvious that the assert should be fulfilled this way.
Extension methods are kind of overlays for the class.
{code:java}
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {}
class B extends A {}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 1
}
usageExtStatic()
{code}
m(Object) is to be seen as if it is a method of B. m(String) is to be seen as
if a method of A. Since B extends A and since B does not define a new
m(String), the extension method for A has to be chosen here.
{code:java}
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {}
class B extends A {
def m(String s){3}
}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 3
}
usageExtStatic()
{code}
In this case m(String) exists on A as extension method, but B defined its own
m(String), which "overwrites" m(String) from A. Neither overwrite nor overload
are the right terms for this I guess.{code:Java}
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {
def m(String s){4}
}
class B extends A {}
class C extends B {}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 1
def c = new C()
assert c.m(new Object()) == 2
assert c.m("") == 1
}
usageExtStatic(){code}
Here, even though A defines m(String), the extension method "shadows" it and
inherits it to B. For C again it is the extension methods only.
I know very long comment, but well... the above was for the issue described
here, to show how it is supposed to work - and does in the dynamic case. The
fix though involves getAt(Map,String) and GString:
{code:java}
class Container {
public someObject = ""
String toString(){ return "Container_" +someObject}
int hashCode() { return 42 + someObject.hashCode() }
boolean equals(o) {
return o!=null && o.class==Container && o.someObject == someObject
}
void newObject(o) {this.someObject = o}
}
def m = new HashMap()
def container = new Container(someObject: "1")
m.put(container, 1)
assert m.get(container) == 1
container.newObject("2")
assert m.get(container) == null
{code}
Here the second get fails even though container itself did not change really.
But since the contained Object did, you cannot access the map value using that
same reference. This is given by Java.
{code:java}
class Container {
public someObject = ""
String toString(){ return "Container_" +someObject}
int hashCode() { return 42 + someObject.hashCode() }
boolean equals(o) {
return o!=null && o.class==Container && o.someObject == someObject
}
void newObject(o) {this.someObject = o}
}
def m = new HashMap()
def container = new Container(someObject: "1")
m.put("$container", 1)
assert m.get("$container") == 1
container.newObject("2")
assert m.get("$container") == null
{code}
In this version it is a similar situation to before, the only change is that we
create now a ton of GString objects. GString though has the same problem that
Container has, hashcode and equals depend on the contained object, which is
subject to change. Examples are GStrings made from Closures and GStrings made
of containers like here. the more idiomatic usage would be:
{code:java}
// Container like before
def m = [:]
def container = new Container(someObject: "1")
m["$container"] = 1 //putAt
assert m["$container"] == 1 //getAt
container.newObject("2")
assert m["$container"] == null //getAt
{code}
This proofed to be difficult to understand for the people, which is why we said
we do not allow that for the static compiler.
I noticed you added getAt(Map,String), to allow assert map["a"] == 1 // not 'x'
But what about the putAt? This will go through InvokerHelper, thus map["a"] = 1
will set the property. I think one is not complete without the other. Right now
{code:java}
class HM extends HashMap {
String a = 'x'
}
def map = new HM()
map["a"] = 1
assert map["a"] == 1
{code}
will fail Groovy because map["a"] sets the property, while map["a"] gets the
map value.
{code:java}
class HM extends HashMap {
String a = 'x'
}
def map = new HM()
def x = "a"
map["$x"] = 1
assert map["$x"] == 1
{code}
did behave just the same. The reason is that in dynamic Groovy putAt(Object,
String) exists as extension method and is preferred over putAt(Map, Object),
since GString is kind of a String. Now before the fix it seems the static
compiler was not seeing GString as a kind of String, thus the Object method is
taken. But if you fixed that, then there is no need to add that getAt method.
There is already one for this case.
was (Author: blackdrag):
>From the comments I am not sure the fix goes in the right direction - of
>course I could be totally wrong here.
{code:Java}
class A {
def m(o){1}
}
class B extends A {
def m(String s) {2}
}
def a = new A()
assert a.m(new Object()) == 1
def b = new B()
assert b.m(new Object()) == 1
assert b.m("") == 2
{code}
in this case it is obvious that the assert should be fulfilled this way.
Extension methods are kind of overlays for the class.
{code:Java}
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {}
class B extends A {}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 1
}
usageExtStatic()
{code}
m(Object) is to be seen as if it is a method of B. m(String) is to be seen as
if a method of A. Since B extends A and since B does not define a new
m(String), the extension method for A has to be chosen here.
{code:Java}
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {}
class B extends A {
def m(String s){3}
}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 3
}
usageExtStatic()
{code}
In this case m(String) exists on A as extension method, but B defined its own
m(String), which "overwrites" m(String) from A. Neither overwrite nor overload
are the right terms for this I guess.
class MExt {
static m(A self, String s) {1}
static m(B self, Object o) {2}
}
class A {
def m(String s) {4}
}
class B extends A {}
class C extends B {}
@groovy.transform.CompileStatic
void usageExtStatic() {
def b = new B()
assert b.m(new Object()) == 2
assert b.m("") == 1
def c = new C()
assert c.m(new Object()) == 2
assert c.m("") == 1
}
usageExtStatic()
{code}
Here, even though A defines m(String), the extension method "shadows" it and
inherits it to B. For C again it is the extension methods only.
I know very long comment, but well... the above was for the issue described
here, to show how it is supposed to work - and does in the dynamic case. The
fix though involves getAt(Map,String) and GString:
{code:Java}
class Container {
public someObject = ""
String toString(){ return "Container_" +someObject}
int hashCode() { return 42 + someObject.hashCode() }
boolean equals(o) {
return o!=null && o.class==Container && o.someObject == someObject
}
void newObject(o) {this.someObject = o}
}
def m = new HashMap()
def container = new Container(someObject: "1")
m.put(container, 1)
assert m.get(container) == 1
container.newObject("2")
assert m.get(container) == null
{code}
Here the second get fails even though container itself did not change really.
But since the contained Object did, you cannot access the map value using that
same reference. This is given by Java.
{code:Java}
class Container {
public someObject = ""
String toString(){ return "Container_" +someObject}
int hashCode() { return 42 + someObject.hashCode() }
boolean equals(o) {
return o!=null && o.class==Container && o.someObject == someObject
}
void newObject(o) {this.someObject = o}
}
def m = new HashMap()
def container = new Container(someObject: "1")
m.put("$container", 1)
assert m.get("$container") == 1
container.newObject("2")
assert m.get("$container") == null
{code}
In this version it is a similar situation to before, the only change is that we
create now a ton of GString objects. GString though has the same problem that
Container has, hashcode and equals depend on the contained object, which is
subject to change. Examples are GStrings made from Closures and GStrings made
of containers like here. the more idiomatic usage would be:
{code:Java}
// Container like before
def m = [:]
def container = new Container(someObject: "1")
m["$container"] = 1 //putAt
assert m["$container"] == 1 //getAt
container.newObject("2")
assert m["$container"] == null //getAt
{code}
This proofed to be difficult to understand for the people, which is why we said
we do not allow that for the static compiler.
I noticed you added getAt(Map,String), to allow assert map["a"] == 1 // not 'x'
But what about the putAt? This will go through InvokerHelper, thus map["a"] = 1
will set the property. I think one is not complete without the other. Right
now{code:Java}
class HM extends HashMap {
String a = 'x'
}
def map = new HM()
map["a"] = 1
assert map["a"] == 1
{code}
will fail Groovy because map["a"] sets the property, while map["a"] gets the
map value.
{code:Java}
class HM extends HashMap {
String a = 'x'
}
def map = new HM()
def x = "a"
map["$x"] = 1
assert map["$x"] == 1
{code}
did behave just the same. The reason is that in dynamic Groovy putAt(Object,
String) exists as extension method and is preferred over putAt(Map, Object),
since GString is kind of a String. Now before the fix it seems the static
compiler was not seeing GString as a kind of String, thus the Object method is
taken. But if you fixed that, then there is no need to add that getAt method.
There is already one for this case.
> 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)