VLSI DV Interview Puzzles · All levels
Invariance of Parameterized Type Assignment
Why does assigning `agent#(req_ext)` to `agent#(req_base)` fail even though `req_ext extends req_base`?
Puzzle
Difficulty: Hard · Puzzle 4 of 6 · Topic: Parameterized Class Puzzles
Why does assigning `agent#(req_ext)` to `agent#(req_base)` fail even though `req_ext extends req_base`?
Code
class req_base; endclass
class req_ext extends req_base; endclass
class agent #(type REQ_T = req_base);
REQ_T last_req;
endclass
initial begin
agent#(req_ext) a_ext = new();
agent#(req_base) a_base;
a_base = a_ext;
endHint
Parameterized classes are invariant in type parameters unless language/library defines covariance.
Step-by-step solution
1) `agent#(req_ext)` and `agent#(req_base)` are different unrelated specialization types.
2) Subtyping of `req_ext` does not imply subtyping of enclosing parameterized class.
3) Assignment is type-incompatible and should fail compile-time type checking.Answer
Answer: Assignment fails because specialization types are invariant; `agent#(req_ext)` is not a subtype of `agent#(req_base)`.
Why candidates get it wrong
Candidates transfer container covariance expectations from other languages into SV/UVM.
Interviewer follow-up
What wrapper/interface pattern would you use if you need heterogeneous specialization storage?