SystemVerilog OOP Mastery · All levels

Specializations, typedef Aliases, and the Per-Specialization Static Gotcha: Exercises

Exercises for Specializations, typedef Aliases, and the Per-Specialization Static Gotcha.

Practice exercises

Exercise 1

Given packet#(byte,8) and packet#(int,8), design a report that prints per-specialization and global creation counts.

Solution

diagram
Print packet#(byte,8)::created and packet#(int,8)::created for per-specialization values, and maintain a separate packet_stats::global_created incremented in every constructor.

Exercise 2

Replace repeated packet#(bit[15:0],32) declarations with a typedef and explain what does not change semantically.

Solution

diagram
Use typedef packet#(bit [15:0], 32) pkt16x32_t;. Readability improves, but static storage behavior remains tied to that single specialization only.

Related topics