Re: Scalar type hints and scalar type name aliases cause confuson
| From: | Michael Wallner | Date: | Tue, 13 Oct 2015 10:42:02 +0000 |
| Subject: | Re: Scalar type hints and scalar type name aliases cause confuson | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88764@lists.php.net to get a copy of this message | ||
On 12/10/15 21:23, Andrea Faulds wrote:
> Hi,
>
> As PHP 7 currently is, if you write this:
>
> public function foo(): integer {
> return 12;
> }
> $bar = foo();
>
> you'll get the following error:
>
> Fatal error: Uncaught TypeError: Return value of foo() must be an
> instance of integer, integer returned
>
> Similarly, if you were to write this:
>
> public function foo(): boolean {
> return true;
> }
> $bar = $foo();
>
> you'd get this:
>
> Fatal error: Uncaught TypeError: Return value of foo() must be an
> instance of boolean, boolean returned
>
...
> Alongside or instead of that, though, we can do two other things to make
> this situation better.
>
> First, we could make type error messages use 'int' and 'bool'. This
> feels somewhat wrong to me since integer and Boolean are the proper
> English names for the types. But we do use the short names in some other
> places, like var_dump, so this isn't so bad. This way, we get this
> slightly clearer error:
>
> Fatal error: Uncaught TypeError: Return value of foo() must be an
> instance of integer, int returned
>
> Second, we could add a helpful note to the TypeError message when the
> type hint is for a class named 'integer' or 'boolean' and the value
> passed was an integer or Boolean, respectively. My patch (based on
> Anthony's original) for the earlier Scalar Type Hinting with Cast RFC
> did this. This would make things even clearer:
>
> Fatal error: Uncaught TypeError: Return value of foo() must be an
> instance of integer, int returned (did you mean to use the type hint
> 'int'?)
>
> Even if we can't reserve the names, I hope we can do the two other
> suggestions in time for release.
Additionally, we could say "instance of class ...".
--
Regards,
Mike