java - Adding generic types to EnumMap -
i'm trying add instances following enummap:
class activeg<d extends gdata, t extends gtemplate> { enummap<gstatetype, gstate<activeg<d,t>> map; .. .. } class tgdata extends gdata {...} class tgtemplate extends gtemplate {....} class activetg extends activeg<tgdata, tgtemplate> { // fails compilation error super.states.put(gstatetype.waiting, new tgwaitingstate<activetg>(this)); ... ... }
the error get:
the method put(gstatetype, gstate<activeg<tgdata,tgtemplate>>) in type enummap<gstatetype,gstate<activeg<tgdata,tgtemplate>>> not applicable arguments (gstatetype, tgwaitingstate<activetg>)
can try me figure out what's missing make work?? thanks!
in brief, generics in java not covariance.
e.g. if have
class parent {...} class child extends parent {...}
foo<child>
not foo<parent>
go code:
your enum map in activetg
expecting gstate<activeg<tgdata,tgtemplate>>
value, giving tgwaitingstate<activetg>
, gstate<activetg>
.
although activetg
is-a activeg<tgdata,tgtemplate>
, gstate<activetg>
not gstate<activeg<tgdata,tgtemplate>>
.
in order solve it, need quite change in type params,
class activeg<d extends gdata, t extends gtemplate, e extends activeg<d,t>> { enummap<gstatetype, gstate<e>> map; }
or make map
type enummap<gstatetype, gstate<? extends activeg<d,t>>
. may not work in situation, example, need retrieve result gstate<activetg>
instead of gstate<activeg<d,t>>
Comments
Post a Comment