Re: Should to_number return a sequence?
- Posted by xecronix 6 days ago
- 414 views
It's a little bit of a pickle. to_number, given it's 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.
hmm... Does to_number have too many valid number formats and/or do too much? IDK, opinions, I guess. I think so. But, there isn't anything stopping someone from writing: (not tested... probably works)
function parse_num(sequence s) atom err = atom(to_number(s), -1) atom val = to_number(s) return {err, val} end function
Or if someone were inclined to be Phix and Euphoria compatible: (also not tested)
function parse_num(sequence s) atom b = is_number(s) -- adopting my is_number function or writing a better one. if b then return {b, to_number(s)} -- maybe return a type checked sequence instead? worth considering. end if return {b, 0} end function