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

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

Leon A created GROOVY-10045:
-------------------------------

             Summary: 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


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)