Why do we use $cast() for checking at run time?

In reply to UVM_LOVE:

What is the value of v3 at compile time? In this simple example, you can see it is 3’b010, but that is only at time 0. What about the following code?

enum {RED=3'b001, YELLOW=3'b010, GREEN=3'100} light;
initial begin
  v3 = 3'b010;      // 2 is equivalent to YELLOW
  $cast(light, v3); // Success, light gets YELLOW

  v3++;             // 3 does not match any enum value
  $cast(light, v3); // Error as the value 3 is not a legal enum value
end

The two calls to $cast() are identical, yet one succeeds at run time, and one gets an error. It would be hard for the compiler to predict this. Replace the v3++ with a call to a function that increments v3, and it is almost impossible for the compiler to decide if the $cast call succeeds. That decision can only be done at run time.