www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does assigning void mean?

reply mark <mark qtrac.eu> writes:
In Adam Ruppe's D Cookbook there're these lines in a ref counting 
example:

RefCountedObject o = void; // What does this mean/do?
o.data = new Implementation();
o.data.refcount = 1;

I don't understand the first line; could someone explain please?
Mar 04 2020
parent reply drug <drug2004 bk.ru> writes:
On 3/5/20 10:47 AM, mark wrote:
 In Adam Ruppe's D Cookbook there're these lines in a ref counting example:
 
 RefCountedObject o = void; // What does this mean/do?
 o.data = new Implementation();
 o.data.refcount = 1;
 
 I don't understand the first line; could someone explain please?
In D all vars are initialized by default. If you use assigning void then the var won't be initialized.
Mar 05 2020
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 5 March 2020 at 08:35:52 UTC, drug wrote:
 On 3/5/20 10:47 AM, mark wrote:
 In Adam Ruppe's D Cookbook there're these lines in a ref 
 counting example:
 
 RefCountedObject o = void; // What does this mean/do?
 o.data = new Implementation();
 o.data.refcount = 1;
 
 I don't understand the first line; could someone explain 
 please?
In D all vars are initialized by default. If you use assigning void then the var won't be initialized.
To expand a bit on this: You probably don't want to initialize things with = void - it can lead to hard-to-track bugs and unexpected behavior. The reasons it's there is almost entirely as an optimization - if you know the variable will be initialized elsewhere void initialization ensure things won't be initialized twice when once is enough, and this can be faster. The other use case is when for whatever reason there is no valid default value, but you still want an instance. Probably in order to fill it with data from somewhere else. This would apply e.g. to structs with disabled parameterless constructors whose contents you are reading from disk. In short, when you know you need to void initialize something, that's when you're ready to use it. Kinda like goto. -- Simen
Mar 05 2020
prev sibling parent mark <mark qtrac.eu> writes:
On Thursday, 5 March 2020 at 08:35:52 UTC, drug wrote:
 On 3/5/20 10:47 AM, mark wrote:
 In Adam Ruppe's D Cookbook there're these lines in a ref 
 counting example:
 
 RefCountedObject o = void; // What does this mean/do?
 o.data = new Implementation();
 o.data.refcount = 1;
 
 I don't understand the first line; could someone explain 
 please?
In D all vars are initialized by default. If you use assigning void then the var won't be initialized.
Thanks, I had read it (in "Learning D" I think), but had already forgotten.
Mar 05 2020