Re: Should to_number return a sequence?
- Posted by xecronix 5 days ago
- 312 views
Why shouldn't it return a number or NaN on failure?
Or does NaN not exist in Eu?
I can't speak definitively to whether or not NaN exists in Euphoria or Phix. But, I can say, I haven't seen that yet... sort of.
-- Phix to_number("x") -- this returns a sequence. Which is Not a Number. -- euphoria to_number("x") -- this returns an atom, 0. Which is number. And given the function name, makes sense. -- euphoria to_number("x", -1) -- this voodoo returns a sequence. Which is Not a Number.
I think SDPringle said it best:
It seems like to_number would return a number by the name itself.
So, even if NaN exists, and somehow NaN was represented by something that isn't a number, a dev would not be wrong expect a number to be returned from a function named to_number.
I've always wondered if every return value in Euphoria should be of { SUCCESS_BOOL, RETURNED_VALUE }, but that seems way too verbose.
I've thought about this too over the years. Not specifically for Euphoria but for my code in any language I use. I've never fully committed to this. But looking back on the industry as a whole I think there is a strong argument for returning {success, value} for most functions that return values. Especially numeric returns and object returns. (since objects can be null in many languages).
{success, value} isn't arbitrary in position. For a dev to get to the value, he has to first step over the success. Hopefully he checks it along the way. Honestly, I could make a good living just going from shop to shop fixing null ref exceptions. This return pattern might have helped drive the "trust but verify" point home. But now, I'm starting to slip past the topic at hand.