Daniel Sun created GROOVY-12319:
-----------------------------------

             Summary: Java compatibility: remaining generic type syntax
                 Key: GROOVY-12319
                 URL: https://issues.apache.org/jira/browse/GROOVY-12319
             Project: Groovy
          Issue Type: Improvement
            Reporter: Daniel Sun


Groovy already accepts most Java generics. Three Java forms still fail — two in 
the parser, one in {{GenericsVisitor}}. Each has a local workaround. Method 
type arguments such as {{Helper.<String>identity(x\)}} already work and are not 
part of this request.

h3. 1. Diamond {{<>}} on an anonymous class

Java 9+ (JEP 213) allows:
{code:java}
Processor<String> p = new Processor<>() {
    public String process(String val) { return val; }
};
{code}

Groovy rejects this with {{Cannot use diamond <> with anonymous inner 
classes}}. Writing  new Processor<String>() \{ ... \}  compiles.

GROOVY-6730 and GROOVY-7159 were false-positive STC errors when diamond was 
_not_ used; they did not add this form.

h3. 2. Qualified parameterized inner types ("rare" types)

Java allows an inner type to keep the outer's type arguments:
{code:java}
class Outer<T> {
    class Inner<U> {}
}
Outer<String>.Inner<Integer> x;
{code}

Groovy fails to parse {{Outer<String>.Inner}} ({{Unexpected input}}). A factory 
that returns {{Inner}} without naming {{Outer<T>.Inner}} is a workaround. The 
same qualification appears in {{o.new Inner<Integer>(42)}}.

h3. 3. Explicit type arguments on constructors, {{this()}} and {{super()}}

Java allows:
{code:java}
class Box {
    <T> Box(T t) {}
}
new <String>Box("x");
{code}

and, on generic constructors, {{<T>this()}}, {{<T>super()}}, 
{{recv.<T>super()}}, and {{outer.new <T>Inner(...)}}.

Groovy fails at {{new <}} with {{Unexpected input: '<'}}. Inference ({{new 
Box("x")}}) is the workaround.

Constructor type arguments are already tracked by GROOVY-10501. The {{this()}} 
/ {{super()}} / inner-{{new}} forms are the same JLS production (JLS 15.9) and 
should be handled together.

h3. Related

* GROOVY-10501 — constructor type arguments (open)
* GROOVY-6730, GROOVY-7159 — diamond + anonymous class, STC false positives 
(fixed)




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to