Reputation: 10220
I'm using enum method name()
to print the string value of an enum variable.
Should I do a null
check first?
Upvotes: 2
Views: 1808
Reputation: 19094
The name()
method cannot return null
. It will return an empty string if the value does not correspond to an enumerated value. It may be advisable to do an empty string check.
Quoting from IEEE std 1800-2012 § 6.19.5.6 Name()
The name() method returns the string representation of the given enumeration value. If the given value is not a member of the enumeration, the name() method returns the empty string.
Upvotes: 3
Reputation: 10220
No, you don't need to worry about a null
enum. However, an enum could be un-initialized if it does not have a state corresponding to 0
For example:
typedef enum {STATE0, STATE1} plain_enum;
typedef enum {VAL0=1, VAL1} special_enum;
module test;
initial begin
plain_enum plain;
special_enum special;
$display("plain: %s", plain.name());
$display("special: %s", special.name());
end
endmodule
Returns:
# plain: STATE0
# special:
Because special_enum
does not have a state corresponding to 0
Edit/re-run the code here: http://www.edaplayground.com/s/4/647
Upvotes: 1