Jump to page: 1 2
Thread overview
What Does @ Mean?
Apr 08
Alex
Apr 08
Kagamin
Apr 12
wjoe
Apr 08
XavierAP
Apr 08
JN
April 08
This is frustrating and makes me feel like a complete newb. Worse, it's impossible to search for. Ever try Googling a single character?

The D documentation also doesn't seem to explain the meaning of this or any other token. Sure, most of them are obvious, but this one eludes me. All I can find is this: https://dlang.org/spec/lex.html#tokens

Would someone please tell me what an at sign (@) means when it's used like this:

bool isLeaf() @property
{
   return children.length == 0;
}

In fact, I have no idea what @ means anywhere other than in an email address. Is this a common thing in contemporary languages?

And while I'm asking, does an underscore have special meaning when used either at the beginning or end of a variable name?

How about a double underscore?

I know underscores are sort of left over from C, but do these characters have special meaning in D or are they simply a convention... like T in a template definition?


April 08
On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
> This is frustrating and makes me feel like a complete newb. Worse, it's impossible to search for. Ever try Googling a single character?
>
> The D documentation also doesn't seem to explain the meaning of this or any other token. Sure, most of them are obvious, but this one eludes me. All I can find is this: https://dlang.org/spec/lex.html#tokens
>
> Would someone please tell me what an at sign (@) means when it's used like this:
>
> bool isLeaf() @property
> {
>    return children.length == 0;
> }
>
> In fact, I have no idea what @ means anywhere other than in an email address. Is this a common thing in contemporary languages?
>
> And while I'm asking, does an underscore have special meaning when used either at the beginning or end of a variable name?
>
> How about a double underscore?
>
> I know underscores are sort of left over from C, but do these characters have special meaning in D or are they simply a convention... like T in a template definition?

Some of the attributes in D go with an "@".

https://forum.dlang.org/post/fiwfcsqmjsndcjixipgz@forum.dlang.org
https://wiki.dlang.org/Language_Designs_Explained
https://dlang.org/spec/attribute.html
April 08
https://dlang.org/spec/function.html#property-functions
April 08
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.
April 08
Well, that was quick!

Thanks Adam, Kagamin, and Alex.
April 08
On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
>
> And while I'm asking, does an underscore have special meaning when used either at the beginning or end of a variable name?

In D, @ is used as Adam has explained as a prefix indicating attributes (either user-defined ones or, confusingly enough, some of the standard ones).

The only other example of language using @, in an almost but not quite completely different way, is C#. It's also a prefix that allows you to define names that would collide with reserved words, for example string @class = "menu"; Of course you should never do this unless you absolutely need for interop.

Underscore prefixes are used in some languages by pure user convention mainly for private members (fields), to avoid name clashing. For example in D you could have a public property length and a private member _length.

Python takes this a step further. Since it supports classes but no public/private visibility at all, users and IDEs have convened to use (one or two) underscore prefixes to signal members that aren't meant to be accessed publicly from outside the class, even if there's nothing stopping you (besides auto code completion not showing them).

For C and C++ the convention (recognized by the standards) is different: names prefixed by any number of underscores are all reserved; basically because the global namespace is so badly polluted already. In this case I've seen some other annoying conventions, for example private member variables being prefixed with m_
April 08
On Monday, 8 April 2019 at 14:19:04 UTC, XavierAP wrote:
> The only other example of language using @, in an almost but not quite completely different way, is C#. It's also a prefix that allows you to define names that would collide with reserved words, for example string @class = "menu"; Of course you should never do this unless you absolutely need for interop.

Java uses @ for annotations too. Pascal uses @ for "address of", like & in D.
April 08
On Monday, 8 April 2019 at 14:19:04 UTC, XavierAP wrote:
> On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
>>
>> And while I'm asking, does an underscore have special meaning when used either at the beginning or end of a variable name?
>
> In D, @ is used as Adam has explained as a prefix indicating attributes (either user-defined ones or, confusingly enough, some of the standard ones).

Yup. Confusion. Yup. :)

> Of course you should never do this unless you absolutely need for interop.

No fear of that. I was just trying to understand the example I cited.

> For example in D you could have a public property length and a private member _length.

This seems like a good practice.

> In this case I've seen some other annoying conventions, for example private member variables being prefixed with m_

The things I missed by not getting my degree... or by not keeping my nose to the grindstone for the last 30-odd years. :)

Thanks, Xavier.
April 08
On Monday, 8 April 2019 at 14:27:11 UTC, JN wrote:

> Java uses @ for annotations too. Pascal uses @ for "address of", like & in D.

Just one of the many reasons I balked at Java... many MANY reasons.

Thanks, JN.
April 12
On Monday, 8 April 2019 at 12:16:13 UTC, Adam D. Ruppe wrote:
> On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
>> 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`).
>

It's not entirely related but another use of the underscore is to make integers more readable. E.g.:

int x = 1_000_000_000_000;

« First   ‹ Prev
1 2