Re: [RFC] Scalar Type Hinting With Casts (re-opening)
| From: | Andrea Faulds | Date: | Wed, 16 Jul 2014 19:52:21 +0000 |
| Subject: | Re: [RFC] Scalar Type Hinting With Casts (re-opening) | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75600@lists.php.net to get a copy of this message | ||
On 16 Jul 2014, at 20:47, Rowan Collins <rowan.collins@gmail.com> wrote:
> 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.
Perhaps this could work?
if (safe_cast((int!)$foobar)) {
$foobar = (int!)$foobar;
} else {
$foobar = 1;
}
With safe_cast() being a pseudo-function à la isset() et al.
I’m not really sure adding a new set of safe casts is really such a good idea; we don’t really
need (int!) even, we could just have can_cast_int_strict (or something like that) given that (int)
and (int!) do the same thing for the non-failure cases.
And perhaps it’d be better just to leave this to scalar type hints, I’m not sure I really want
to add this stuff...
--
Andrea Faulds
http://ajf.me/