Not logged in
Log in now
Create an account
Subscribe to LWN
Pencil, Pencil, and Pencil
Dividing the Linux desktop
LWN.net Weekly Edition for June 13, 2013
A report from pgCon 2013
Little things that matter in language design
Cracks in the Foundation (PHP Advent)
Posted Dec 21, 2011 2:37 UTC (Wed) by Richard_J_Neill (subscriber, #23093)
BUT, what do you do with an integer-function that normally returns an integer (negative, positive, or zero) when it needs to return an error?
There are several ways to do it; of which I think that C's strtol() is the worst possible. Some might suggest returning an object, but that's logically equivalent. PHP has adopted the general convention that any function that fails will return false; I think this is actually quite sensible once one knows to expect it.
As for the casting rules of "0.0" vs 0.0, it's rather a perverse example, which shouldn't happen in real-life.
Posted Dec 21, 2011 4:26 UTC (Wed) by nybble41 (subscriber, #55106)
Return success or failure (or a more specific error code), and store the result in a reference parameter? That's the standard C approach. If the possibility of failure exists, you'll need to check for that first anyway before using the result. For uncommon failure modes, in languages which support it, alternate continuations (including, but not limited to, C++-style exception handling) offer a more efficient solution.
> PHP has adopted the general convention that any function that fails will return false; I think this is actually quite sensible once one knows to expect it.
That's great so long as you don't need to return false in a non-failure situation... The Common Lisp solution to this is rather elegant: return two values, the first being the result or false, and the second (which you can ignore) indicating success or failure. If you know that successful results can't be false you can use the normal return value, but the extended status is there if you need it.
Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds