Re: [RFC] [Discussion] Shorter Attribute Syntax Change
| From: | tyson andre | Date: | Wed, 29 Jul 2020 16:29:58 +0000 |
| Subject: | Re: [RFC] [Discussion] Shorter Attribute Syntax Change | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111248@lists.php.net to get a copy of this message | ||
Hi internals,
I had thought of another alternative syntax -
/** @@MyAttribute */, which would solve
some of the problems I mentioned about #[.
```
namespace My\NS;
use Other\MyAttribute;
/**
* Use @@ or << at the start of a line. (To be determined)
* Resolve the names in the comment relative to the scope of the function, like the current
attribute implementation.
* @@MyAttribute(expr, expr)
* Extra *s are allowed
*** @@AnotherAttribute
* Tokenizing should continue until a matching ) is found
* @@YetAnotherAttribute(
* expr
*)
*/
function my_function() {
}
```
Pros:
- Doc comments are already the recommended way to associate information with a named element.
- Most code modifying tools will preserve doc comments
- This can go on the same line as a closure/short arrow function without lexing ambiguity.
- Frameworks can use Reflection*->getDocComment() and update their checks to also
allow @@ for attribute-like annotations when run in PHP 7.
They don't have to repeat both @@MyAttribute and /** @MyAttribute
*/ or risk it going out of sync
(Symfony would already have to parse the use clauses to find out what
@MyAttribute resolves to for Symfony attributes)
- Does not affect lexing outside of the doc comment
- I don't believe @@ is common. To minimize the impact on ascii art, this can be initially
limited to @@ at the start of a comment line (spaces and *s) immediately followed by \
or the start of an identifier
Cons:
- Need to run the lexer twice on a T_DOC_COMMENT token, but only if the comment string contains
@@ and is associated with an element.
Emitting T_DOC_COMMENT_START and T_DOC_COMMENT_END and T_DOC_COMMENT_INNER and
T_DOC_COMMENT_ATTRIBUTE
T_DOC_COMMENT_ATTRIBUTE_ERROR might make this technically feasible (similar to heredoc having
complicated logic)
- I prefer @@ and <<>> over this proposal long-term, after php
7.4 support is dropped.
But I prefer /** @@MyAttr */ over #[MyAttr].
- Would end up emitting parse errors for some existing code in the second lexer pass.
- Syntax highlighting might be hard to implement in editors, but probably tractable.
I don't remember seeing this proposed before. I believe @@ isn't used by mainstream
frameworks in doc comments right now, but correct me if it is.
Thanks,
- Tyson