Re: Type locked variables
| From: | Guy Marriott | Date: | Wed, 16 Nov 2016 00:06:15 +0000 |
| Subject: | Re: Type locked variables | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96905@lists.php.net to get a copy of this message | ||
I think it would make more sense for typed properties to exist first, and
it so happens that there was a recent RFC for this:
https://wiki.php.net/rfc/typed-properties.
That RFC failed by a very small
margin.
I would suggest you read through the discussions on the mailing list
regarding that RFC for some common arguments to typing in PHP.
On Wed, Nov 16, 2016 at 12:44 PM, Michael Morris <tendoaki@gmail.com> wrote:
> Perhaps it's time to revisit the idea of allowing variables to have their
> types locked down. The keywords needed are already reserved. So...
>
> string $a = "hello";
> int $b = 5;
>
> Once declared this way the variable's type won't change unless it gets
> unset. In normal mode PHP will coerce any assignment to the variable to the
> desired type. If declare strict types is on then assigning the wrong type
> to a variable would raise a type error.
>
> Note this would simplify one of the use cases of setters - insuring the
> class member is of a valid type.
>
> The idea of function overloading reminded me of this. I'd consider being
> able to do this sort of type lockdown to be prerequisite to having
> overloaded functions.
>
> Since this approach is entirely opt in I don't think it would make the
> language harder to learn.
>