On Fri, Jul 10, 2009 at 02:44:52AM +0200, troels knak-nielsen wrote: [...] > > For example, instead of: > > function addFive(int $x) { > return $x + 5; > } > > You would simply do: > > function addFive(is_numeric $x) { > return $x + 5; > } > > Since $x is guaranteed to be numeric, it is safe to to arithmetic on > it. No reason to explicitly type cast here. >
Please let me state that this is the greatest idea I read since the type hinting discussion began (and the only one I personally like, as I find all the other proposals really disappointing). I'd like to see some real debating about this syntax proposed by troels: function addFive(is_numeric($x)) { ... } Because I really like the idea of passing other parameters: function check_multiple($a, $b) { return !($a % $b); } function doSomething(check_multiple($a, 3)) { // assume that $a is a multiple of 3 } This solves all the problems I've understood so far: - BC breaks for new reserved keywords 'int', 'float', ...: there is no need for them anymore - weak typing vs strict typing: function cast_bool(&$value) { $value = (bool) $value; return true; } function weak_type_hinted(cast_bool($a)) { /* assume that $a is bool, no fatal errors */ } - Nullable values: function is_int_nullable($val) { return is_int($val) || is_null($val); } function int_or_null(is_int_nullable($a)) { /* either $a is int or it's null */ } - This will also make the error handling consistent where the classic type hinting wouldn't cover a particular use case: function foo(int $a) { ... } function bar($b) { if ($b is not appropriated parameter) trigger_error("Invalid parameter"); } // a type of error (E_RECOVERABLE or TypeCheckException or whatever) foo("inappropriate_parameter"); // a custom error, which might be *slightly different* from the one generated by the inner php engine bar("another_inappropriate_parameter"); I think this gives the perfect freedom to the developers and makes everyone happy. I know your concerns about third party libraries that you might have to deal with, but if you trigger one of those errors, then it means you are not respecting the APIs, which means there is a bug in your application. -- Giovanni Giacobbi -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php