generics - "Unexpected token" using lower-bounded wildcard (Java) -
i have along lines of:
interface foo<t> { //... lines [0,45]... /*line 46*/ <r, x super t&r> list<x> weave(r value); //... }
but intellij reporting:
- error:(46, 18) java: > expected
- error:(46, 19) java: illegal start of 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 problem? not allowed bind name lower bound? or allowed use r&x
expression in upper bound?
changing to
interface foo<t> { //... lines [0,45]... /*line 46*/ <r> list<? super t&r> weave(r value); //... }
yields
- error(46, 31) java: > expected
- error(46, 32) java: '(' expected
- error(46, 33) java: illegal start of type
by reading of specification, super
can used wildcard , can't captured type variable; see jls 4.5.1. similarly, &
valid in type variables, not type arguments, , type variables can't use super
.
after having thought it, here's explanation: reason type variable eliminate explicit casting improve type safety. when declare type parameter super foo
, you're saying it's okay parameter any superclass of foo
. means , including object
, , have no safe way presume objects type satisfies bound, , there's no information whatsoever contained within named type variable; wildcard , can call hashcode()
or tostring()
, nothing type-specific.
Comments
Post a Comment