Meaningful identifiers and other multi-token keywords

ryuukk_ ryuukk.dev at gmail.com
Wed Sep 25 18:02:36 UTC 2024


On Tuesday, 24 September 2024 at 20:37:36 UTC, Quirin Schroll 
wrote:
> D’s has 4 places in the grammar where meaningful identifiers 
> are used instead of keywords:
> - Pragmas
> - Traits
> - Linkages
> - Scope guards
>
> For pragmas and traits, this is total non-issue as they have 
> special and dedicated keywords. For linkages and scope guards, 
> there will be rough edges if we make `(Type)` be a well-formed 
> `BasicType`. The reason is that `extern(C)` could mean `extern` 
> plus the basic type `(C)`, where `C` denotes e.g. a dummy 
> class; or `scope (exit) x = 10;` with the intention not to 
> assign `x`, but to declare `x` as a `scope` variable of type 
> `exit`. In general, you could ask: Why would one write such 
> code? and you’d be correct.
>
> The issue is with the argument to `extern` and `linkage` being 
> identifiers. For linkage, it’s implementation defined which 
> ones are supported, and they’re not just identifiers (e.g. 
> `C++` and `Objective-C`), however, with scope guards, there are 
> only `exit`, `success`, and `failure`.
>
> I want to suggest moving the parsing of scope guards and 
> linkages to the lexer, i.e., if the lexer sees `scope`, `(`, 
> any one of the identifiers `exit`, `success`, or `failure`, and 
> `)`, that is a scope guard and is treated as a single token.
>
> The same with `extern(C)` – it will never be seen as anything 
> but a linkage. It’s a multi-token keyword.
>
> Possibly, we can handle other cases alike, e.g. `static 
> assert`, `static foreach`, and `auto ref`. By all accounts, 
> their meaning isn’t derived from composing the semantics of the 
> parts.
>
> What do you think?

I love scope guards, i use them all the time, however, they are 
both painful to type and makes code ugly to read

Perhaps `scope(exit)` and `scope(failure)` should be renamed, 
`defer` and `errDefer`

Solves your problem, and mine


More information about the Digitalmars-d mailing list