开发者

Generic type arguments in derived classes

Groovy seems to be unable to compile a class where I've derived from a Generic base class and overloaded a method returning a generic array. The same example in Java appears to compile correctly, which is surprising as I expected Groovy to have source-level compatibility [ref].

This can be reproduced with the following adapter example. This example can also be executed online at the awesome Groovy Web Console.

class Person {}

​abstract class Base​Adapter<T> {
    public T[] getAll() {
        return getAllInternal();
  开发者_StackOverflow  }

    protected abstract T[] getAllInternal()
}

class PersonAdapter extends BaseAdapter<Person> {
    protected Person[] getAllInternal() {
        return new Person[0];
    }
}​

Compiling this produces the message:

Script1.groovy: 12: The return type of Person[] getAllInternal() in PersonAdapter is incompatible with [Ljava.lang.Object; getAllInternal() in BaseAdapter
. At [12:5]  @ line 12, column 5.
    protected Person[] getAllInternal() {
    ^

1 error

Have I got the syntax incorrect? Or, is this a Groovy issue?


I expected Groovy to have source-level compatibility.

The source-level compatibility is maybe 90% but not perfect (nor is this a goal).

Have I got the syntax incorrect? Or, is this a Groovy issue?

Probably a bug. Please file a bug report.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜