SystemVerilog OOP Mastery · All levels
No Operator Overloading: Exercises
Exercises for No Operator Overloading.
Practice exercises
Exercise 1
Given a packet class used in a scoreboard, define APIs to support equality check, ordering by sequence id, and map key generation.
Solution
diagram
Add equals(packet), compare(packet), and hash() methods; document that == remains handle identity and should not be used for semantic compare.Exercise 2
Replace an API that currently expects expr-like usage (a+b) with an explicit method style in SV.
Solution
diagram
Introduce add(rhs) and accumulate(rhs) methods, update all call sites, and include unit tests showing equivalent behavior to the original operator intent.