Reputation: 4789
I've a code snippet like following in my testbench
function void write_to_port( my_data_type_base data );
my_data_type_extended data_ext;
if(!$cast(data_ext, data));
`uvm_error(get_type_name(), "failed to cast");
`uvm_info(get_name(), $psprintf("data_ext :\n%s", data_ext.sprint()), UVM_MEDIUM)
// write data_ext out to another port....
endfunction
When I run it, I'm getting uvm_error as "failed to cast." I'm not quire sure why $cast is not returning 1. As you can see, I'm printing out the extended class data item after casting with uvm_info. I can see that it's being cast properly. If I don't use $cast with if condition, I don't get any runtime error. Isn't it a good coding practice to always use if with dynamic cast to check if $cast is returning 1 ?
What might be the reason behind cast not returning 1 in above case?
Upvotes: 1
Views: 1473
Reputation: 35923
I think the semicolon on the line with 'if' does not belong?
I think that consumes the if statement, and then the uvm_error executes regardless of how if evaluates:
if(!$cast(data_ext, data)); <- no semicolon
`uvm_error(get_type_name(), "failed to cast");
Upvotes: 2