www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to understand different attribute styles?

reply timepp <tongjunhui live.cn> writes:
there is " disable", using   as prefix;
there is "__gshared", using __ as prefix;
there is also "align", not using prefix.

I failed to summarize a rule here. Can anyone tell the underlined 
philosiphy?
Oct 17 2016
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 17 October 2016 at 14:12:33 UTC, timepp wrote:
 there is " disable", using   as prefix;
 there is "__gshared", using __ as prefix;
 there is also "align", not using prefix.

 I failed to summarize a rule here. Can anyone tell the 
 underlined philosophy?
It's an inconsistency of the language. see http://forum.dlang.org/post/hfmulninvghjntqkpguk forum.dlang.org
Oct 17 2016
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, October 17, 2016 14:12:33 timepp via Digitalmars-d-learn wrote:
 there is " disable", using   as prefix;
 there is "__gshared", using __ as prefix;
 there is also "align", not using prefix.

 I failed to summarize a rule here. Can anyone tell the underlined
 philosiphy?
There really isn't one. __gshared is probably the way that it is, because it's not something much of anything but C bindings should be using (it exists solely for compatibility with C global variables). So, it's treated as a compiler thing (like __traits) rather than a normal attribute. But as for , what that pretty much comes down to is that Walter didn't want to add more keywords. So, new ones got put on them (and all of this was well before user defined attributes came along). In general, folks don't like the fact that there is no consistency between what has and what doesn't, but no one can come up with a scheme that's actually fully consistent (at least, not without slapping on all attributes or having it on none), and it would break a ton of code if any of them were changed now, so it's highly unlikely that any of them will ever change. So, you just learn which ones have and which don't, and then it mostly doesn't matter anymore, as annoying as it may be from an aesthetic point of view. - Jonathan M Davis
Oct 17 2016
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/17/16 10:12 AM, timepp wrote:
 there is " disable", using   as prefix;
 there is "__gshared", using __ as prefix;
 there is also "align", not using prefix.

 I failed to summarize a rule here. Can anyone tell the underlined
 philosiphy?
terms without adornments are keywords. They are used in parsing to anchor the parser, and can only mean what they are intended to mean by the language. This means they are reserved by the language, and can't be used by users of the language. Terms with __ are also keywords, but are marked that way because they are used in unusual circumstances, were intended to be replaced with a more formal keyword, or were chosen as not to interfere with any existing symbols (i.e. added after much D code already existed). The language tries not to add keywords any more because it breaks any code that uses it. There aren't many of these. In general, you should not use symbols with two underscores because those are expected to be reserved by the language or compiler. It's not a hard-fast rule, but I'd expect there to never be any new keywords that don't start with __. The terms with the sign are attributes, and can only be used as attributes. They are never storage classes, types, or keywords in general. In this space, the language has many pre-defined attributes, and you can't use them to mean something different (as attributes) once the language has defined it. Think of these as pre-defined symbols. The nice thing about these are that you can use them as regular symbols without the sign, and it doesn't change anything. As long as you don't intend to use them as actual attributes, you should be fine. However, I'd recommend not to do this as this can be confusing to anyone reading your code and not knowing the pre-defined attributes. Note that attributes were added much later in the language, and most of the predefined attributes were added before user defined attributes were supported. So I expect not many more language defined attributes will be added unless absolutely necessary. You may ask, why the difference between e.g. safe and pure? Historical reasons really. It should really be pure, but pure was a keyword from very early. So there is a bit of that. -Steve
Oct 17 2016