Re: Scalar Type Hints v0.4

From: Date: Wed, 18 Feb 2015 13:31:35 +0000
Subject: Re: Scalar Type Hints v0.4
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-83066@lists.php.net to get a copy of this message
Hi François, On Wed, Feb 18, 2015 at 3:02 PM, François Laupretre <francois@php.net> wrote: >> De : Andrey Andreev [mailto:narf@devilix.net] >> >> Consider the following signature: >> >> function foo(int $bar) {} >> >> In the case of a *string* representation of a hexadecimal number, the >> following would error only on the last iteration with a weak hint, and >> on the very first if it was a strict hint: >> >> for ($i = 0; $i < 11; $i++) >> { >> foo(base_convert($i, 10, 16)); >> } > > You're right. An hexa string with no leading '0x' and containing decimal digits > only cannot be recognized as hexa. But I keep thinking that, balancing pros and cons, it's not > enough to justify strict mode. Maybe I'm wrong and additional use cases will make me change my > mind, but I consider hexa with no prefix as an edge-case. > > I don't say that's the right solution but the problem can be solved at the > base_convert() level. If we support '0x' strings as hexadecimal numbers, it can generate > the '0x' prefix, which removes ambiguity for PHP and any other software that would have to > interpret the string. Unfortunately, it would probably have to be explicitly required through an > option because of BC. No perfect solution here. > In real-world applications, base_covert() would hardly ever be the culprit and I don't want to change its current behavior. I only used base_convert() in the example because that allowed the least amount of code written to display the problem. > Another argument some may consider weak : I'm also afraid that, in your example, the user > seeing an error raised by strict mode could change its code to 'foo((int)base_convert', > definitely hiding the real bug, even for 11 and up. So, Rasmus is right when he says strict mode can > sometimes and indirectly be counter-productive. Debugging shouldn't be considered as error > suppression only, but it is the case more than often. > Sorry, but I do consider that to be a weak argument ... We can't help users who's only concern is eliminating error messages, we can only help those that understand them. Cheers, Andrey.

« previous php.internals (#83066) next »