Posted Dec 19, 2011 10:32 UTC (Mon) by khim
In reply to: Why is it a mess?
Parent article: Cracks in the Foundation (PHP Advent)
I don't see the point in allowing anything that isn't either true or false in an if statement/expression.
The point is expressiveness, as usual. You can do anything you want without such things (see Turing tarpit), but this kind of misses the point.
I happen to like the way the STL solved this problem in std::find. If an element is found, return an iterator to it. If it's not found, return a past-the-end iterator.
This is good kludge for the statically typed language with a lot of limitations. They could not use just plain NULL because they wanted to make sure iterators may be anything, so they invented this scheme. Still not sure why you think it's better then simple "iterator or #f": in C++ case you often need to process iterator somehow (if caller which needs the iterator does not want to know about your map), in case of LISP you can just return result as is.
In Haskell, one would typically return a 'Maybe' value, which is either "Nothing" or "Just x", where x is the number you're looking for. One then uses pattern matching to distinguish the cases.
And all thus additional unneeded manipulations are good
I dislike languages without static typing because they leave too much to the runtime, but then if we are already in realm of dynamic languages it's stupid not to use the fact that you can return objects of different types to your advantage: why do you use language with dynamic typing or duck typing if you only use things available in statically typed language???
to post comments)