RE: [PHP-DEV] Scalar Type Hints v0.4
| From: | François Laupretre | Date: | Wed, 18 Feb 2015 13:30:22 +0000 |
| Subject: | RE: [PHP-DEV] Scalar Type Hints v0.4 | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-83064@lists.php.net to get a copy of this message | ||
> De : Robert Stoll [mailto:php@tutteli.ch]
>
> Strict mode is useful in the sense that it prevents unnecessary implicit
> conversions (which are costly) and it improves readability.
> Following an example:
>
> function foo(string $x, int $y){
> bar(1);
> return strstr($x,"hello", $y);
> }
>
> function bar(float $a){}
>
> After adding the implicit conversions the code would look as follows:
>
> function foo(string $x, int $y){
> bar((float) 1);
> return strstr($x, "hello", (bool) $y);
> }
>
> function bar(float $a){}
>
> In strict mode the original code would not be valid (rightly so IMO).
Actually, your example is partially invalid because strict-typing radicals now propose to add a (int
-> float) exception to so-called strict mode (which proves the approach is flawed, IMHO).
You don't propose a strict-mode alternative in your example. OK, it generates conversions and
should fail. Now, how would you write the same code using strict-mode and without adding casts which
would do exactly the same, but slower.
If you just mean there's an undetected bug, you're right, but, IMO, a C-like syntax like
PHP's cannot disable (int -> bool) implicit conversion.
A partial solution can be brought by a set of strict types I am planning to define in the single
(not so weak) mode approach I am working on (something like 'int!',
'float!',...). This would allow people who know what they're doing to demand
zval-type-based strict checks, arg by arg. It can be used for performance reasons and for the rare
cases where zval type really matters (sorting, for instance). This would be available to internal
and userland functions.
Regards
François