Re: Scalar type hints and scalar type name aliases causeconfuson
| From: | Andrea Faulds | Date: | Tue, 13 Oct 2015 13:44:49 +0000 |
| Subject: | Re: Scalar type hints and scalar type name aliases causeconfuson | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88775@lists.php.net to get a copy of this message | ||
Hi Derick,
Derick Rethans wrote:
On Mon, 12 Oct 2015, Andrea Faulds wrote:Oh yeah, that's a bit of an issue. Since I changed it, PHP 7 doesn't say 'double' in its type error messages any more, but there's still going to be people who'd expect that to work. So, my patch to reserve and produce errors includes 'double'.you'll get the following error:<snip>Fatal error: Uncaught TypeError: Return value of foo() must be an instanceof integer, integer returnedyou'd get this:What about double vs float? Another common combination.Fatal error: Uncaught TypeError: Return value of foo() must be an instanceof boolean, boolean returned
This is also an issue for argument typehints:Exactly the same issue, yes. I just used return types for my examples, sorry if that was unclear.
TypeError: Argument 1 passed to foo() must be an instance of integer, integer given, called in /tmp/scalar.php on line 7 in /tmp/scalar.php on line 3 (I also think, this reads odd with two times "in <filename> on line <x>")It does. Could be improved slightly by the addition of a comma before the second 'in'.
for: <?php declare(type=strict); function foo(integer $a) { } foo(42); ?>Yeah. My current patch does this:To avoid confusion, I would like it if we reserve 'integer' and 'boolean' alongside 'int' and 'bool', and make them produce an error if used.With a parse error mentioning 'illegal token' or something like that?
./sapi/cli/php -r 'function foo(): long {}'
Fatal error: 'long' is not a valid type hint, use 'int' instead in Command line code on line 1If you try to use it as a the name of a class/interface/trait, it produces the usual error for reserved class name.
Well, that's what we have RCs for. And there are a few more coming up!Yes, perhaps I shouldn't give up just yet.
I'd find that nannying :)It *is* a bit annoying, especially if you actually intended to have a class by that name.
I'd prefer reservation of integer and boolean, but a better error message is okay too. I'd go with something like: Fatal error: Uncaught TypeError: Return value of foo() must be an instance of class boolean, scalar boolean returnedThat sounds good, albeit a little verbose. Thanks! -- Andrea Faulds http://ajf.me/