Re: Should to_number return a sequence?
- Posted by euphoric (admin) 6 days ago
- 396 views
It's a little bit of a pickle. to_number, given its name, should simply return a number. But since 0 is a number, that opens up the door for confusion about the meaning of the return value. I think return values that differ in size and type seems peculiar though. And since try/catch isn't an option...
So, what are options?
- to_number crashes on failure. Valid option. But, without an is_number function, that seems a little unforgiving.
- to_number always returns a sequence. It's debatable about what's in the sequence, but 0 or a failure position in element 1 and a number or 0 in element 2 seems reasonable. Or maybe just 1 or 0 in the first element? Or maybe the first element is a number type? (0=NAN, 1=Integer, 2=Float, 3=Currency maybe?) Then, every call has a consistent return value. The next question is if to_number is a valid name for the function. (maybe parse_num might be better?)
- Do nothing. It's fine the way it is.
I think parse_num() is a great replacement name for that function. It can return a sequence. { TRUE/FALSE, RETURNED_VALUE }. RETURNED_VALUE is only relevant if the first element is TRUE.
Isn't there a value() function that basically does this already?
(I searched the manual for value but could not find anything. Not even a search through Google worked.)
I've always wondered if every return value in Euphoria should be of { SUCCESS_BOOL, RETURNED_VALUE }, but that seems way too verbose.