You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/04/20 20:52:00 UTC

[jira] [Assigned] (GROOVY-10045) Type parameters on Map subtypes are erased

     [ https://issues.apache.org/jira/browse/GROOVY-10045?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles reassigned GROOVY-10045:
------------------------------------

    Assignee: Eric Milles

> Type parameters on Map subtypes are erased
> ------------------------------------------
>
>                 Key: GROOVY-10045
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10045
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 3.0.8
>            Reporter: Leon A
>            Assignee: Eric Milles
>            Priority: Major
>              Labels: @CompileStatic
>
> Arbitrary property names on Maps are treated as .get(propName) and work with @CompileStatic.
> Unfortunately, the resulting type of the property value becomes Object when using a subtype of Map with specified type parameters
> Example
> {code:groovy}
> class IntMap extends HashMap<String, Integer> { }
> IntMap bar() {
>   IntMap map = new IntMap();
>   map.put("value", 42);
>   return map;
> }
> bar().value.intValue(); // [Static type checking] - Cannot find matching method java.lang.Object#intValue(). Please check if the declared type is correct and if the method exists.
> bar().get("value").intValue(); // works
> {code}
> This example works if IntMap has a type parameter as seen below
> {code:groovy}
> class IntMap<T> extends HashMap<String, Integer> { }
> IntMap<Object> bar() {
>   IntMap<Object> map = new IntMap<>();
>   map.put("value", 42);
>   return map;
> }
> bar().value.intValue(); // works
> bar().get("value").intValue(); // works
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)