> -----Original Message-----
> From: Andrea Faulds [mailto:a...@ajf.me]
> Sent: Thursday, October 23, 2014 12:42 AM
> To: Stas Malyshev
> Cc: Zeev Suraski; Dmitry Stogov; PHP Internals
> Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions
>
>
> > On 22 Oct 2014, at 22:31, Stas Malyshev <smalys...@sugarcrm.com>
> wrote:
> >
> > Hi!
> >
> >> Are you opposed to the existence of ext/filter given it has
> >> FILTER_VALIDATE_INT, a “primitive for handling type conversions”?
> >
> > FILTER_VALIDATE_INT is an option for a filter_var function, and it is
> > not introducing any new rules for handling types in the engine.
>
> Nor is this.
>
> > What you are proposing is not like FILTER_VALIDATE_INT, it's like (int).
>
> I don’t see the difference:
>
>     $x = filter_var(FILTER_VALIDATE_INT, $foo);
>
>     $x = to_int($foo);
>
> They’re very similar, except the latter has slightly different rules, is
> shorter,
> and if some people (possibly me) get their way, might throw an exception.

Andrea,

You're analyzing this from a purely technical perspective.  There's a world
of difference between these two if we take into account the human element.

The former provides a very clear idea that you're in the context of
filtering and validation.  The whole point of filtering is to, well, filter
out stuff.

The latter, on the other hand, looks like 'PHP, please convert this to an
int for me, the way you do these things, please.'  I know what it does
without looking at the docs.  But actually, I'm wrong - it does something
different, and has a behavior which is fundamentally inconsistent with the
rest of the language that could easily result in broken apps.  to_int()
doing one thing, and (int) doing something completely different?  That's a
major #fail.

If we use more explicit naming, this argument goes away completely.
lossless_int() does a fairly good job at self-describing what it does, and
completely prevents creating the false assumption that it's identical to
PHP's type conversion rules.  The chances the average developer would think
that lossless_int() and (int) are identical is slim to non-existent.

Regarding the fact that it's longer - I think we've made the decision to be
explicit and not try to shorten things up a very long time ago
(unfortunately not long *enough* ago).   Clarity is a heck of a lot more
important than a few extra chars.  Not to mention that in order to properly
use this new mechanism - people will have to write a lot of defensive error
handling code around it - otherwise, what's the point.  People who have a
good reason to use these lossless conversions would not be deterred from 6
extra characters - even at the most minimalistic error handling scenario
we're talking about dozens of extra characters compared to a simple (int)
cast.

Zeev

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to