groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mirco <mirch...@gmail.com>
Subject Unexpected behaviour for getAt operator operloading with static compilation
Date Sat, 21 Oct 2017 16:06:09 GMT
Hi,
I've encountered a strange compile error with the getAt method and the
@CompileStatic annotation.
Below some scripts to reproduce the error with the GroovyConsole:

import groovy.transform.CompileStatic
@CompileStatic
class Pippo {
    int getAt(String pluto) { return 1 }
    int plus(String paperino) { return 1 }
}
@CompileStatic
public class Demo {
    public static void main(String[] args) {
        def pippo = new Pippo()
        int i = pippo['a']          // [Static type checking] - Cannot
assign value of type java.lang.Object to variable of type int
        //int i = pippo.getAt('a')  // Works
        //int i = pippo + 'a'       // Works
    }
}
Demo.main()

At first I thought of some general issue with operator overloading but the
plus method does not seem to be affected (last commented line above).
Morevover, my actual code involves generics and in this case new errors
came up even with the cast:

import groovy.transform.CompileStatic
@CompileStatic
class Pippo<K,V> {
    V paperino
    V getAt(K pluto) { return paperino }
}
@CompileStatic
public class Demo {
    public static void main(String[] args) {
        Pippo<String, Integer> pippo = new Pippo<>()
        pippo.paperino = 313
        int i = (int) pippo['a']       // groovy.lang.MissingPropertyException:
No such property: a for class: Pippo
        // int i = pippo.getAt('a')    // works
        println i
    }
}
Demo.main()

If the two generic types are the same (ex. both Integer):

BUG! exception in phase 'class generation' in source unit 'ConsoleScript0'
At line 12 column 23
On receiver: pippo with message: getAt and arguments: 1
This method should not have been called. Please try to create a simple
example reproducing this error and filea bug report at
https://issues.apache.org/jira/browse/GROOVY

import groovy.transform.CompileStatic
@CompileStatic
class Pippo<K,V> {
    V paperino
    V getAt(K pluto) { return paperino }
}
@CompileStatic
public class Demo {
    public static void main(String[] args) {
        Pippo<Integer, Integer> pippo = new Pippo<>()
        pippo.paperino = 313
        int i = (int) pippo[1]       // groovy.lang.MissingPropertyException:
No such property: a for class: Pippo
        // int i = pippo.getAt('a')    // works
        println i
    }
}
Demo.main()

Is there some known issue about getAt and CompileStatic or simply I
misunderstood its use?

Thanks,

M

Mime
View raw message