Re: [VOTE] Shorter Attribute Syntax Change
| From: | tyson andre | Date: | Thu, 13 Aug 2020 17:21:55 +0000 |
| Subject: | Re: [VOTE] Shorter Attribute Syntax Change | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111508@lists.php.net to get a copy of this message | ||
Hi internals,
One thing I hadn't thought of before was how this would interact with parameters (with or
without constructor property promotion).
Many coding styles would end up putting annotations on the same line for parameters (in addition to
closures)
E.g. I've commonly done that and seen that done in Java:
https://stackoverflow.com/questions/30237266/java-parameter-annotations
```
class X {
public function __construct(
@@MyImmutable public bool $x,
private bool $flag = false,
) {}
public function __construct(
@[MyImmutable] public bool $x,
private bool $flag = false,
) {}
// This comments out the first parameter entirely in php7, silently leading to different
behavior in php 7
public function __construct(
#[MyImmutable] public bool $x,
private bool $flag = false,
) {}
}
```
As Theodore said, this RFC doesn't include enough examples to give a comprehensive
understanding of how #[] would be implemented and what the implementation implies, which the
original shorter attribute syntax did https://wiki.php.net/rfc/shorter_attribute_syntax#alternative_syntax
(e.g. this gets parsed as
$f1 = $f2 = $object = new foo(); in php 7 and would not warn
with your patch (from the mailing list) in php8. No patch for #[Attr] is linked in the RFC
document.)
```
$f1 = #[ExampleAttribute] function () {};
$f2 = #[ExampleAttribute] fn() => 1;
$object = new #[ExampleAttribute] class () {};
foo();
```
Even if the RFC was amended after the vote was started,
I don't think that people who had already voted would see the changes or read all of the
numerous emails in this thread.
> Changes lexing of remaining tokens indicates that a newly introduced token changes the
> behaviour of existing tokens.
> In the example of #[ the occurance of a # would have previously
> always signified a comment, but that now changes to become an attribute instead when followed by
> [.
That's similar to saying that the ?? token would have previously indicated a
? for a nullable type/ternary, but would change to become a null coalescing operator
when followed by ?.
It understates/omits mentions of the changes to lexing this can cause, which I mentioned in https://externals.io/message/111218#111239
```
?php
// This example echoes the rest of the source code in php 7
// and echoes "Test" in php 8.
#[DeprecationReason('reason: <https://some-website/reason?>')]
function main() {}
const APP_SECRET = 'app-secret';
echo "Test\n";
```
```
// yields false in php 7, yields a function in php 8
function generator() {
yield #[MyCustomAttribute('
false;
// ']function() {};
}
```
Regards,
- Tyson