Why do some attributes start with '@' while others done't?

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 21 15:18:16 PST 2016


On Thursday, 21 January 2016 at 23:05:51 UTC, Dibyendu Majumdar 
wrote:
> I am puzzled as to why there is @nogc on the one hand and 
> simply nothrow on the other? Why are some attributes prefixed 
> with '@' while others aren't?
>
> Regards

Short answer: It's for historical reasons; it would not be done 
this way today, but cannot be changed without breaking valid code 
already in the wild.

Longer answer: The ones prefixed with @ are not keywords. This is 
good, because it means that they are not reserved, and may be 
used as identifiers as well.

This will compile fine:

void nogc(int nogc) @nogc nothrow {
}

This won't, though:

void nothrow(int nothrow) @nogc nothrow {
}

When the process of designing D began, people didn't realize just 
how many attributes would end up being added to the language. At 
some point, it became clear that it was both undesirable and 
unnecessary to forbid all attribute names from being used for 
other purposes elsewhere in D's grammar. The solution was to 
begin prefixing @ to new attributes.

Adding the @ to the old attributes was discussed as well, but it 
didn't seem worth the code breakage.

A revision of D that wasn't constrained by backwards 
compatibility would almost certainly either require all 
attributes to be prefixed by @, or change the grammar such that 
attribute names could be reused as identifier names without 
introducing ambiguities.


More information about the Digitalmars-d mailing list