AW: [PHP-DEV] What do we need strict scalar type hints for?
| From: | Robert Stoll | Date: | Mon, 02 Feb 2015 18:41:38 +0000 |
| Subject: | AW: [PHP-DEV] What do we need strict scalar type hints for? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81626@lists.php.net to get a copy of this message | ||
Hi Dimitry,
> -----Ursprüngliche Nachricht-----
> Von: Dmitry Stogov [mailto:dmitry@zend.com]
> Gesendet: Montag, 2. Februar 2015 10:13
> An: PHP Internals; Andrea Faulds; Nikita Popov
> Betreff: [PHP-DEV] What do we need strict scalar type hints for?
>
> hi,
>
> could you please write down few use cases, when strict scalar type hints are really useful.
>
> Thanks. Dmitry.
I think strict types are beneficial in all those places where you would put manual tests to ensure
that the passed value is of a certain type and only of this type - removing the hassle (ok, I did
not add anything to the discussion so far ^^). Right now, I mainly think of functions which expect
an int - especially in the domain of time and money. I would expect an int and I would not want
that a float can be passed without warning and get silently converted to an int (loosing precision
in the domain of time or money can be crucial).
However, IMO it is not necessary to have a strict mode as presented by Andrea -- I think it would be
good enough if we use the same widening rules as in Java, C# etc. and be strict otherwise. I am
aware of that with the support of BigInteger we would have a widening problem (BigInteger to float)
but could be solved by a BigDecimal as in Clojure.
Ah... I now something I would definitely want to be strict. I guess a typical bug, which is made by
many beginners, is using the result of strpos in an if statement without using the identity operator
-- btw. would the strict mode affect operators and control structures as well? Would somehow be the
logical consequence but I suppose the impact on performance would be too big. Or wouldn't it?
Back to the topic, if I expect a bool as parameter I would definitely not want it to be
automatically converted since this is a sink for bugs.
Hope that helps.
Cheers,
Robert