Unexpected token using lower bound wildcard (Java)

I have something similar:

interface Foo<T> {
    //... lines [0,45]...

/*line 46*/ <R,X super T&R> List<X> weave(R value);
    //...
}

But IntelliJ is reporting:

>Error: (46,18) Java: > expected > error: (46,19) Java: illegal startup type > error: (46,26) Java: '('expected > error: (46,28) Java: < identifier > expected > error: (46,29) Java:' l 'expected > error: (46,43) Java: < identifier > expected

What's the problem? Don't I allow names to be bound to the lower limit? Or do I only allow R & X expressions at the upper limit?

Change it to

interface Foo<T> {
    //... lines [0,45]...

/*line 46*/ <R> List<? super T&R> weave(R value);
    //...
}

yield

>Error (46,31) Java: > expected > error (46,32) Java: '(' expected > error (46,33) Java: illegal startup type

Solution

Through my reading of the specification, super can only be used with wildcards and cannot be captured in type variables; See JLS 4.5 1. Similarly, & only valid in type variables, not type parameters, and type variables cannot use super

After consideration, this is my explanation: the reason for type variables is to eliminate explicit conversion to improve type safety When you declare that a type parameter is super foo, you say that this parameter can be any super class of foo This means that it can be anything including object, so you have no safe method to assume that its type meets the scope of the object, so there is no information contained in the named type variable; You are just a wildcard and can call hashcode () or toString (), but there is no type specific

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>