Question regarding inheritance and any
Brian Goetz
brian.goetz at oracle.com
Wed Jan 7 23:16:29 UTC 2015
Right. First, type hierarchies have to be any-fied from the top down.
You can have
interface A<any T> { }
interface B<any T> extends A<T> { }
and
interface A<any T> { }
interface B<T> extends A<T> { }
but not
interface A<T> { }
interface B<any T> extends A<T> { }
If you think of this in terms of a type bound (any T == Object|Value),
this is the equivalent of "type parameter T is not within its bound."
Similarly, at the use site, the compiler will reject new A<int>() if int
is not within the "bound" of A's tvar.
On 1/7/2015 5:33 PM, Sven Reimers wrote:
> Hi,
>
> trying some simple inheritance I tried
>
> public class Valhalla {
>
> public static void main (String[] args) {
>
> List<int> intPoints = new ArrayList<>();
> System.out.println(intPoints);
>
> }
>
> public static interface List<any T> { }
>
> public static class ArrayList<any T> implements List<T> { }
>
> }
>
> If I remove the any from the ArrayList<any T> I get the following compile
> error:
>
> incompatible types: cannot infer type arguments for ArrayList<>
> List<int> intPoints = new ArrayList<>();
> ^
> reason: no instance(s) of type variable(s) T exist so that ArrayList<T>
> conforms to List<int>
> where T is a type-variable:
> T extends Object declared in class ArrayList
> 1 error
>
> Having given this failure some thought I am now thinking this is
> intentional so that every implementation of an anyfied super
> class/interface has to actively opt in to provide support for anyfied types.
>
> Is this the intention?
>
> Thanks for clarifying and your patience answering all those questions...
>
> -Sven
>
More information about the valhalla-dev
mailing list