java - Parsing <T extends Interface<T>> and ending a potential infinite cycle -
consider class definition starts
class pokemon extends playable<pokemon>
this similar more common class pokemon implements comparable<pokemon>
, imposes total ordering on pokemons.
although have been seeing , writing time, realized (after fielding question) theoretical standpoint @ least, there risk of infinite loop in parsing if 1 not careful.
consider this:
step 1: compiler or classloader tries parse (or load) pokemon
, sees needs parse playable<.>
first.
step 2: compiler realizes because playable
parameterized pokemon
, needs load or parse pokemon
. find ourselves going step 1, , never-ending cycle established.
in practice, know not case, because works. how cycle broken? theory @ end of step 2, compiler or classloader stops , use "reference" pokemon instead of pulling pokemon source code. don't know enough javac
or classloaders
affirm this. weigh in?
this similar "loop" in declaration:
class linkedlistnode { private string data; private linkedlistnode next; ... }
in order process private linkedlistnode next
field, compiler needs know linkedlistnode
type. not need have complete type @ point, because type name provides enough information declare field.
similarly, when declare generic class refers own type part of inheritance structure, compiler not need complete type finish parsing declaration.
Comments
Post a Comment