What Does @ Mean?
Adam D. Ruppe
destructionator at gmail.com
Mon Apr 8 12:16:13 UTC 2019
On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
> Would someone please tell me what an at sign (@) means when
> it's used like this:
>
> bool isLeaf() @property
In that case, it means nothing. We just defined the word to be
`@property`, with the @ included. So it is just part of the name.
Same with @safe, @trusted, @system, @nogc, and a few others built
into the language.
It was arbitrary decided to put the @ in the words so it wouldn't
conflict with existing variable names like `int safe;`
In some other contexts though, it just indicates to the parser
that it is a user-defined attribute instead of continuing the
declaration with a name. In these, it is a prefix to another user
value. For example:
struct MyCustomAttribute {} // perfectly ordinary struct
@MyCustomAttribute() void func() {}
In that function, the @ before the name MyCustomAttribute tells
the compiler that it is just an annotation instead of a return
value or the defined name of the new function.
So, two purposes for the @: it is just part of some built-in
keywords, and it can indicate you are using a user-define d name
as an attribute/annotation instead of as a return value, etc.
> And while I'm asking, does an underscore have special meaning
> when used either at the beginning or end of a variable name?
Nothing special there, you are allowed to use the _ anywhere in a
name. Some people use it as a convention on private members,
unused members, or keyword conflicts (like `body_` so the
compiler doesn't see it as the keyword `body`).
But it is just a user convention, it doesn't mean anything
special to the compiler... except:
> How about a double underscore?
A leading double underscore is somewhat special. Those are
allowed in any name, but those are reserved for compiler
implementation details.
So like `__ctor` is the special name the compiler uses for
constructors. You can call it (though since this is an internal
compiler implementation detail you are choosing to exploit, it is
liable to break at any time) or define stuff with these names to
do various hacks.
You should avoid __names though since the compiler reserves them
for its own use.
More information about the Digitalmars-d-learn
mailing list