digitalmars.dip.ideas - Changes to User-Defined Attributes syntax
- Andrey Zherikov (97/97) Nov 18 ## Recap
- user1234 (12/23) Nov 20 Yes, I noticed a long time ago that you can declare
Current syntax (from [lang ref](https://dlang.org/spec/attribute.html#uda)): ``` UserDefinedAttribute: ( TemplateArgumentList ) TemplateSingleArgument Identifier ( NamedArgumentList[opt] ) TemplateInstance TemplateInstance ( NamedArgumentList[opt] ) TemplateSingleArgument: Identifier FundamentalType CharacterLiteral StringLiteral InterpolationExpressionSequence IntegerLiteral FloatLiteral true false null this SpecialKeyword ``` After expanding `TemplateSingleArgument`, we have this: ``` UserDefinedAttribute: ( TemplateArgumentList ) CharacterLiteral StringLiteral InterpolationExpressionSequence IntegerLiteral FloatLiteral true false null this SpecialKeyword FundamentalType Identifier Identifier ( NamedArgumentList[opt] ) TemplateInstance TemplateInstance ( NamedArgumentList[opt] ) ``` Replace comma-separated list of expressions ``` ( TemplateArgumentList ) ``` with single expression ``` ( TemplateArgument ) ``` Example: instead of ` (A,B,C)`, one should write ` (A) (B) (C)`. I believe this is much clearer because it forces single attribute per single ` `-expression. This will also reduce cognitive load on weird things like ` (_=>(2,4),2,4,_=>2,4)`. Replace ``` FundamentalType Identifier Identifier ( NamedArgumentList[opt] ) TemplateInstance TemplateInstance ( NamedArgumentList[opt] ) ``` with parts of [PostfixExpression](https://dlang.org/spec/grammar.html#PostfixExpression) and [PrimaryExpression](https://dlang.org/spec/grammar.html#PrimaryExpression): ``` UserDefinedAttribute: ... UserDefinedAttributeExpression UserDefinedAttributeExpression: FundamentalType Identifier . Identifier TemplateInstance . TemplateInstance UserDefinedAttributeExpression . Identifier UserDefinedAttributeExpression . TemplateInstance UserDefinedAttributeExpression ( NamedArgumentList[opt] ) ``` This will allow writing `!`/`.`-chained expressions without parentheses: ` A!B.C(D)` instead of ` (A!B.C(D))` currently. Remove [undocumented behavior](https://forum.dlang.org/thread/hnjcuvuzytbuvgfdhfkd forum.dlang.org) from compiler which allows using lambda in UDA without parentheses: ```d _ => 2 int a; ``` In this case, one should be forced to use parentheses: ```d (_ => 2) int a; ```
Nov 18
On Tuesday, 19 November 2024 at 04:26:17 UTC, Andrey Zherikov wrote:Current syntax (from [lang ref](https://dlang.org/spec/attribute.html#uda)): ``` UserDefinedAttribute: ( TemplateArgumentList ) TemplateSingleArgument Identifier ( NamedArgumentList[opt] ) TemplateInstance TemplateInstance ( NamedArgumentList[opt] ) [...]Yes, I noticed a long time ago that you can declare ```d alias F = float; F int i; ``` but not ```d float int i; ```
Nov 20