Re: [RFC][DISCUSSION] Strong Typing Syntax
| From: | Andreas Hennings | Date: | Wed, 03 Jan 2018 17:10:26 +0000 |
| Subject: | Re: [RFC][DISCUSSION] Strong Typing Syntax | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101509@lists.php.net to get a copy of this message | ||
This proposal contains some interesting ideas, which I see as separate:
1. A syntax to declare the type of local variables.
2. A syntax to declare the type of object properties.
3. Preventing local variables, object properties and parameters to
change their type after initialization/declaration.
For me the point 3 is the most interesting one.
I think the other points are already discussed elsewhere in some way,
although they are clearly related to 3.
Point 3 would be a BC break, if we would introduce it for parameters.
Current behavior: https://3v4l.org/bjaLQ
Local variables and object properties currently cannot be types, so
point 3 would not be a BC break for them, if we introduce it together
with 1 and 2.
But then we would have an inconsistency between parameters and local
vars / object properties.
What we could do to avoid BC break is to introduce
declare(fixed_parameter_types=1) in addition to
declare(strict_types=1).
For local variables and object properties, the type would always be fixed.
But for parameters, it would only be fixed if the
declare(fixed_parameter_types=1) is active.
Maybe to make it less verbose, we could say declare(strict_types=2),
which would mean the combination of both those things?
Or some other type of shortcut.
I think we will have to think about shortcuts like this if we
introduce more "modes" in the future.
> Currently the var keyword is used to formally declare a variable.
Are you talking about local variables?
In which PHP version? https://3v4l.org/o0PFg
Afaik, currently var is only used for class/object properties from the
time when people did not declare the visibility as
public/protected/private.
> If the type is omitted, scalar is assumed. If Fleshgrinder's scalar RFC is
> accepted then it would make sense to allow programmers to explicitly
> declare the variable as a scalar, but in any event when the type is omitted
> scalar must be assumed for backwards compatibility.
If no type is specified, then "mixed" should be assumed, not "scalar".
Assuming "scalar" would be a BC break, and it would be confusing.
On 3 January 2018 at 10:03, Michael Morris <tendoaki@gmail.com> wrote:
> On Wed, Jan 3, 2018 at 3:50 AM, Niklas Keller <me@kelunik.com> wrote:
>
>> Hey Michael,
>>
>> I don't think the BC break is acceptable. You argue that scalar type
>> declarations are relatively new, but in fact they're already years old now.
>> They're used in most PHP 7+ packages. Even if changing types might be
>> discouraged, it still happens a lot.
>>
>
> Hmm. Well, that aspect of this can be dropped. What about the rest of it?