cooltoad
cooltoad

Reputation: 11

Casting Operator on a null string in Specman

I am developing some RTL checkers in Specman E and using type conversion to process a command line argument.

The code is as follows,

if(get_symbol("SWITCH").as_a(uint) > 10) {

  do something;

};

The command line has an option SWITCH=10. The problem is the test fails if there is no switch with the error.

*** Error: Looking for a number but found 'an empty string' 

How can I avoid this error? Is there is way to give some default value to the casting operator so that whenever it processes null it returns a zero instead? Ofcourse I can first check whether the string is null and only then proceed. But I want to avoid that extra line and see if I can play around with as_a.

Thanks.

Upvotes: 0

Views: 160

Answers (2)

hevangel
hevangel

Reputation: 93

I am afraid you can't work around this problem without having the extra line.

The .as_a() is function of the string object, but get_symbol() will return a null string if SWITCH is not defined in the command line.

Upvotes: 0

Yuri Tsoglin
Yuri Tsoglin

Reputation: 963

This is how casting from string to number works - if it's not a number, you get an error. You could wrap it in a try block to catch the error, but if you know for sure that the only other possibility is an empty string (and not any other non-number string), the best solution is simply to check whether the string is empty.

Upvotes: 1

Related Questions