Re: What Does @ Mean?
On Friday, 12 April 2019 at 10:01:40 UTC, wjoe wrote: 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; And, I suspect, to make numbers easier to translate between English Canadian and French Canadian: Canadian: $1,200,333.45 Canadien: $1.200.333,45
Re: What Does @ Mean?
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;
Re: What Does @ Mean?
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.
Re: What Does @ Mean?
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.
Re: What Does @ Mean?
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.
Re: What Does @ Mean?
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_
Re: What Does @ Mean?
Well, that was quick! Thanks Adam, Kagamin, and Alex.
Re: What Does @ Mean?
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.
Re: What Does @ Mean?
https://dlang.org/spec/function.html#property-functions
Re: What Does @ Mean?
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/fiwfcsqmjsndcjixi...@forum.dlang.org https://wiki.dlang.org/Language_Designs_Explained https://dlang.org/spec/attribute.html
What Does @ Mean?
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?
Re: What does ! mean?
On Wednesday, 27 September 2017 at 14:34:06 UTC, Eugene Wissner wrote: On Wednesday, 27 September 2017 at 14:23:01 UTC, Ky-Anh Huynh wrote: See also the following chapter in Ali's book: http://ddili.org/ders/d.en/templates.html This chapter is what hooked me with D. Naming that chapter as "Templates for Human Beings" won't be an exaggeration.
Re: What does ! mean?
On Wednesday, 27 September 2017 at 17:58:27 UTC, Ali Çehreli wrote: On 09/27/2017 08:33 AM, Ky-Anh Huynh wrote: > [...] Wissner wrote: > [...] The fact that such an important operator is explained so late in the book is due to the book's strong desire to have a linear flow. [...] ustad, guess you can still write the new ed. :-)
Re: What does ! mean?
On 09/27/2017 03:06 PM, Mengu wrote: ustad, guess you can still write the new ed. :-) Since you're still around, one of these days... :) Ali
Re: What does ! mean?
On 09/27/2017 08:33 AM, Ky-Anh Huynh wrote: > On Wednesday, 27 September 2017 at 14:34:06 UTC, Eugene Wissner wrote: >> >> See also the following chapter in Ali's book: >> http://ddili.org/ders/d.en/templates.html > > Thanks a lot. I will keep reading :) The fact that such an important operator is explained so late in the book is due to the book's strong desire to have a linear flow. Although binary ! appears before the templates chapter as to!int, format!"%s", etc., I decided to get to templates only after going through everything that could be templatized. (For example, even interfaces can be templates.) I don't claim this is the best choice but I'm happy that some people said that they found the linear flow useful. (I know that others would find it boring. :) ) If I were to write the book again, one other thing that I would explain earlier would be UFCS, and I would use much more of it in the examples. (UFCS was added to the language after I wrote most of the book.) Ali
Re: What does ! mean?
On Wednesday, 27 September 2017 at 14:23:01 UTC, Ky-Anh Huynh wrote: Can you please explain and give any link where I can learn more about these things? Thanks a lot. http://nomad.so/2013/07/templates-in-d-explained/
Re: What does ! mean?
On Wednesday, 27 September 2017 at 14:34:06 UTC, Eugene Wissner wrote: See also the following chapter in Ali's book: http://ddili.org/ders/d.en/templates.html Thanks a lot. I will keep reading :)
Re: What does ! mean?
On Wednesday, 27 September 2017 at 14:23:01 UTC, Ky-Anh Huynh wrote: Hi, I am from Ruby world where I can have `!` (or `?`) in method names: `!` indicates that a method would modify its object (`foo.upcase!` means `foo = foo.upcase`). ( I don't know if there is any official Ruby documentation on this convention though. ) In D I see `!` quite a lot. I have read the first 50 chapters in Ali's book but nowhere I see a note on `!`. It's about the compile thing, isn't it? E.g, ``` foo = formattedRead!"%s"(value); ``` But I also see `!` for some map/filter invocations. It's quite confusing me. Can you please explain and give any link where I can learn more about these things? Thanks a lot. See also the following chapter in Ali's book: http://ddili.org/ders/d.en/templates.html
Re: What does ! mean?
There are two types of arguments in D. The runtime one (which you are well aware of) and the compile time one. A compile time argument is a constant passed in during construction of a symbol. But here is the thing, it isn't just limited to functions, you can have it on classes as well. --- class Foo(bool b) { bool get() { return b; } } void main() { Foo!true v = new Foo!true; assert(v.get); } --- Same logic going on. --- bool get(bool b)() { return b; } void main() { assert(get!true == true); } --- It can do more than a simple boolean being passed in, but that is the gist. Templates are wonderful (also read the spec!).
What does ! mean?
Hi, I am from Ruby world where I can have `!` (or `?`) in method names: `!` indicates that a method would modify its object (`foo.upcase!` means `foo = foo.upcase`). ( I don't know if there is any official Ruby documentation on this convention though. ) In D I see `!` quite a lot. I have read the first 50 chapters in Ali's book but nowhere I see a note on `!`. It's about the compile thing, isn't it? E.g, ``` foo = formattedRead!"%s"(value); ``` But I also see `!` for some map/filter invocations. It's quite confusing me. Can you please explain and give any link where I can learn more about these things? Thanks a lot.