Re: [RFC] Scalar Type Hinting With Casts (re-opening)

From: Date: Wed, 16 Jul 2014 19:47:48 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-75599@lists.php.net to get a copy of this message
On 15/07/2014 20:44, Andrea Faulds wrote:
PHP has pseudo-functions, maybe we could use that format? cast_int($foo)? Actually, perhaps the solution is an ahead-of-time check:
     if (safe_cast($foo, int)) {
         $bar = (int!)$foo;
     } else {
         $bar = 1;
     }
I think this would be quite nice - keeps things relatively short without introducing so much punctuation that it starts looking like a Perl script ;) The only problem is that either you need a whole set of functions (safe_int($foo), safe_float($foo), etc) or you need some format for that second argument, which gets you into some odd meta-validation: if you take a string, or an integer (expected to be in the form of a constant like PHP_INT), then it would be possible to write safe_cast($foo, $bar), and now we need rules on validating $bar, and what happens if it's wrong. The magic form with a keyword like you've written avoids that, but it feels kind of weird - like when SQL date functions take things like "day" and "hour" as barewords rather than strings. Maybe I'm overthinking it though. -- Rowan Collins [IMSoP]

« previous php.internals (#75599) next »