You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Byron Foster <by...@base2.cc> on 2015/12/27 09:18:25 UTC

Problem overloading bracket operator with CompileStatic

Given the following code:

——————————————————————————————————-
package rankspin

import groovy.transform.CompileStatic

trait SimpleMap<K, V> {
  abstract V getAt(K k)
}

class Foo implements SimpleMap<String, String> {
  String getAt(String k) {return "bar"}
}

@CompileStatic
void go(SimpleMap map) {
  println map["a"]
}

Foo foo = new Foo()
go(foo)
——————————————————————————————————-

I get the following exception:

Caught: groovy.lang.MissingPropertyException: No such property: a for class: rankspin.Foo
groovy.lang.MissingPropertyException: No such property: a for class: rankspin.Foo
	at rankspin.scratch$go.callCurrent(Unknown Source)
	at rankspin.scratch.run(scratch.groovy:19)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

If I take @CompileStatic out it works.  It seems that with compile static Groovy is not honoring the overloaded bracket operators with getAt and setAt.  Am I missing something here?


Also,  in the actual code where this issue is occurring I get a:

Threw: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack
msg: No such property: password for class: rankspin.web.MyRequest

With no stack trace.  Even in the example code above it does not indicate the code site where the error occurred.  is this correct?

Thanks!




Re: Problem overloading bracket operator with CompileStatic

Posted by Byron Foster <by...@base2.cc>.
> On 2015-12-27, at 11:51 , Jochen Theodorou <bl...@gmx.org> wrote:
> 
>> trait SimpleMap<K, V> {
>>   abstract V getAt(K k)
>> }
>> 
>> class Foo implements SimpleMap<String, String> {
>>   String getAt(String k) {return "bar"}
>> }
> 
> 
> if the getAt in Foo is not overriding the abstract getAt in SimpleMap, then this should not even compile. Assuming it does compile, the methods should be overridden, thus it should work. And even if getAt(Object) is used by the static compiler, it should work...
> 
> But... and here is the problem... There is a DGM getAt(String) (http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Object.html#getAt%28java.lang.String%29) as well, which is a shortcut for property access. And now comes part of the argumentation I had in the previous mail. The compiler sees onlny the type SimpleMap, does knows only getAt(Object) (declared on SimpleMap) and getAt(String) (from DGM). Since the String version is the most fitting one and since there is no calling mechanism for subclasses to override a DGM methods in a virtual way as it exists in dynamic Groovy, the compiler will cause the effect of inheritance to be ignored. That we are looking at a trait here is actually only of secondary importance. It could have been an interface and we should face the same problem (if not, that would be actually a bug)
> 
> This is a limitation of the static compiler atm.

Thanks Jochen and Dinko for taking a detailed look at this.  The behavior does work as expected if calling the .getAt method directly and avoiding the [] operator in the go() method. No big loss, I was just partial to the slicker syntax of using the brackets, and it seemed appropriate since it behaves like a Map.

You can’t tell from my example, but the stack trace points to the go() call, but not the site of the exception.  And like I said, in the actual code where this happened there was no stack trace at all, just the error message.  Fortunately, the message is pretty descriptive so easy to track down.  I don’t know why my actual code generates  MissingPropertyExceptionNoStack, but the example does not.  Anyway, it’s an obscure corner case, just thought I would point it out.

First project using Groovy, enjoying it so far.  Thanks!

Re: Problem overloading bracket operator with CompileStatic

Posted by Jochen Theodorou <bl...@gmx.org>.
On 27.12.2015 17:54, Dinko Srkoč wrote:
[...]
> Umm ... Jochen, are you sure this is right? I'm rather convinced that
> this should work in both Java (using interfaces instead of traits) and
> Scala ... and by extension, I would be inclined to call the above
> behaviour a bug. Being statically compiled or not, isn't this the case
> of dynamic dispatch, which should work in any OO language?

on second view... wow... sorry Byron and thanks Dinko for pointing that 
out. I somehow did see quite different code. So let's take it slow

> trait SimpleMap<K, V> {
>    abstract V getAt(K k)
> }
>
> class Foo implements SimpleMap<String, String> {
>    String getAt(String k) {return "bar"}
> }


if the getAt in Foo is not overriding the abstract getAt in SimpleMap, 
then this should not even compile. Assuming it does compile, the methods 
should be overridden, thus it should work. And even if getAt(Object) is 
used by the static compiler, it should work...

But... and here is the problem... There is a DGM getAt(String) 
(http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Object.html#getAt%28java.lang.String%29) 
as well, which is a shortcut for property access. And now comes part of 
the argumentation I had in the previous mail. The compiler sees onlny 
the type SimpleMap, does knows only getAt(Object) (declared on 
SimpleMap) and getAt(String) (from DGM). Since the String version is the 
most fitting one and since there is no calling mechanism for subclasses 
to override a DGM methods in a virtual way as it exists in dynamic 
Groovy, the compiler will cause the effect of inheritance to be ignored. 
That we are looking at a trait here is actually only of secondary 
importance. It could have been an interface and we should face the same 
problem (if not, that would be actually a bug)

This is a limitation of the static compiler atm.

bye Jochen

Re: Problem overloading bracket operator with CompileStatic

Posted by Dinko Srkoč <di...@gmail.com>.
On 27 December 2015 at 14:36, Jochen Theodorou <bl...@gmx.org> wrote:
> On 27.12.2015 09:18, Byron Foster wrote:
>>
>> Given the following code:
>>
>> ——————————————————————————————————-
>> package rankspin
>>
>> import groovy.transform.CompileStatic
>>
>> trait SimpleMap<K, V> {
>>    abstract V getAt(K k)
>> }
>>
>> class Foo implements SimpleMap<String, String> {
>>    String getAt(String k) {return "bar"}
>> }
>>
>> @CompileStatic
>> void go(SimpleMap map) {
>>    println map["a"]
>> }
>>
>> Foo foo = new Foo()
>> go(foo)
>
>
> so basically overloaded getAt in subclass plus static compilation and method
> call on the base class. Well... You have to keep in mind, that there is a
> big difference in method dispatch when it comes to static and dynamic typing
> in Groovy. In dynamic Groovy, Groovy will take the runtime type of an object
> to choose the method. In your case, the call site is the map["a"] aka
> map.getAt("a") line. The static compiler will only see a SimpleMap. The
> static compiler cannot know that this SimpleMap is maybe a Foo providing a
> better fitting getAt(String) method. And since the compiler cannot know
> that, it will take the getAt(K) (aka getAt(Object)) method from SimpleMap.
> Even if you had tried SimpleMap<String>, it would not have changed a thing.
>
> In other words, a static compiler like the one of Groovy (or the one of Java
> and most other static languages cannot do this).

Umm ... Jochen, are you sure this is right? I'm rather convinced that
this should work in both Java (using interfaces instead of traits) and
Scala ... and by extension, I would be inclined to call the above
behaviour a bug. Being statically compiled or not, isn't this the case
of dynamic dispatch, which should work in any OO language?

Cheers,
Dinko

>
> [..]
>>
>> Threw:
>> org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack
>> msg: No such property: password for class: rankspin.web.MyRequest
>
>>
>>
>> With no stack trace.  Even in the example code above it does not indicate
>> the code site where the error occurred.  is this correct?
>
>
> This is not correct, no. The static compiler should not have used the
> dynamic way to get a property, because that is was is producing the stack
> less exception, which is normally caught before it reaches groovy code again
> and transformed into a proper exception. Since the static compiler is not
> really supposed to use those methods, it is no wonder the proper unwrapping
> is missing as well.
>
> bye Jochen
>

Re: Problem overloading bracket operator with CompileStatic

Posted by Jochen Theodorou <bl...@gmx.org>.
On 27.12.2015 09:18, Byron Foster wrote:
> Given the following code:
>
> ——————————————————————————————————-
> package rankspin
>
> import groovy.transform.CompileStatic
>
> trait SimpleMap<K, V> {
>    abstract V getAt(K k)
> }
>
> class Foo implements SimpleMap<String, String> {
>    String getAt(String k) {return "bar"}
> }
>
> @CompileStatic
> void go(SimpleMap map) {
>    println map["a"]
> }
>
> Foo foo = new Foo()
> go(foo)

so basically overloaded getAt in subclass plus static compilation and 
method call on the base class. Well... You have to keep in mind, that 
there is a big difference in method dispatch when it comes to static and 
dynamic typing in Groovy. In dynamic Groovy, Groovy will take the 
runtime type of an object to choose the method. In your case, the call 
site is the map["a"] aka map.getAt("a") line. The static compiler will 
only see a SimpleMap. The static compiler cannot know that this 
SimpleMap is maybe a Foo providing a better fitting getAt(String) 
method. And since the compiler cannot know that, it will take the 
getAt(K) (aka getAt(Object)) method from SimpleMap. Even if you had 
tried SimpleMap<String>, it would not have changed a thing.

In other words, a static compiler like the one of Groovy (or the one of 
Java and most other static languages cannot do this).

[..]
> Threw: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack
> msg: No such property: password for class: rankspin.web.MyRequest
 >
> With no stack trace.  Even in the example code above it does not indicate the code site where the error occurred.  is this correct?

This is not correct, no. The static compiler should not have used the 
dynamic way to get a property, because that is was is producing the 
stack less exception, which is normally caught before it reaches groovy 
code again and transformed into a proper exception. Since the static 
compiler is not really supposed to use those methods, it is no wonder 
the proper unwrapping is missing as well.

bye Jochen