www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does ! mean?

reply Ky-Anh Huynh <saigon example.net> writes:
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.
Sep 27 2017
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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!).
Sep 27 2017
prev sibling next sibling parent reply Eugene Wissner <belka caraus.de> writes:
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
Sep 27 2017
next sibling parent reply Ky-Anh Huynh <saigon example.net> writes:
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 :)
Sep 27 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Sep 27 2017
parent reply Mengu <mengukagan gmail.com> writes:
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. :-)
Sep 27 2017
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Sep 27 2017
prev sibling parent Arun Chandrasekaran <aruncxy gmail.com> writes:
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.
Sep 27 2017
prev sibling parent Gary Willoughby <dev nomad.so> writes:
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/
Sep 27 2017