Raw types and subtyping
- by Dmitrii
We have generic class
SomeClass<T>{ }
We can write the line:
SomeClass s= new SomeClass<String>();
It's ok, because raw type is supertype for generic type. But
SomeClass<String> s= new SomeClass();
is correct to. Why is it correct? I thought that type erasure was before type checking, but it's wrong.
From Hacker's Guide to Javac
When the Java compiler is invoked with default compile policy it performs the following passes:
parse: Reads a set of *.java source files and maps the resulting token
sequence into AST-Nodes.
enter: Enters symbols for the definitions into the symbol table.
process annotations: If Requested, processes annotations found in
the specified compilation units.
attribute: Attributes the Syntax trees. This step includes name
resolution, type checking and constant folding.
flow: Performs data ow analysis on the trees from the previous step.
This includes checks for assignments and reachability.
desugar: Rewrites the AST and translates away some syntactic sugar.
generate: Generates Source Files or Class Files.
Generic is syntax sugar, hence type erasure invoked at 6 pass, after type checking, which invoked at 4 pass. I'm confused.