www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is variable void?

reply John Chapman <johnch_atms hotmail.com> writes:
Is there any way of determining whether a variable has been 
initialized or not? For example, if something is declared like 
this:

   int x = void;

can I check if it's void before I use it, say, in a function it's 
been passed to?
Nov 25
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 25/11/2017 3:34 PM, John Chapman wrote:
 Is there any way of determining whether a variable has been initialized 
 or not? For example, if something is declared like this:
 
    int x = void;
 
 can I check if it's void before I use it, say, in a function it's been 
 passed to?
`` = void;`` isn't null. Don't treat it as such. It is a low level detail where you _will_ initialize it, just in a smarter way. There is no conditions tied directly to it.
Nov 25
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
 Is there any way of determining whether a variable has been 
 initialized or not? For example, if something is declared like 
 this:
nope. It'd be indistinguishable from the user just happening to initialize it to some random value.
Nov 25
parent John Chapman <johnch_atms hotmail.com> writes:
On Saturday, 25 November 2017 at 15:38:15 UTC, Adam D. Ruppe 
wrote:
 nope. It'd be indistinguishable from the user just happening to 
 initialize it to some random value.
Thanks. I'll got with .init instead.
Nov 25
prev sibling next sibling parent crimaniak <crimaniak gmail.com> writes:
On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
 Is there any way of determining whether a variable has been 
 initialized or not? For example, if something is declared like 
 this:

   int x = void;

 can I check if it's void before I use it, say, in a function 
 it's been passed to?
You can use Nullable!int
Nov 26
prev sibling parent reply codephantom <me noyb.com> writes:
On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
 Is there any way of determining whether a variable has been 
 initialized or not? For example, if something is declared like 
 this:

   int x = void;

 can I check if it's void before I use it, say, in a function 
 it's been passed to?
// ---------------------------------- module test; import std.stdio; import std.typecons; // see: https://dlang.org/phobos/std_typecons.html#Nullable void main() { Nullable!int x; // requires: import std.typecons assert(x.isNull); writeln("x is ", x); x = 1; assert(!x.isNull); writeln("x is ", x); x.nullify(); // Forces x back to a null state. assert(x.isNull); writeln("x is ", x); } // ----------------------------------
Nov 26
parent reply bauss <jj_1337 live.dk> writes:
On Monday, 27 November 2017 at 02:12:40 UTC, codephantom wrote:
 On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman 
 wrote:
 Is there any way of determining whether a variable has been 
 initialized or not? For example, if something is declared like 
 this:

   int x = void;

 can I check if it's void before I use it, say, in a function 
 it's been passed to?
// ---------------------------------- module test; import std.stdio; import std.typecons; // see: https://dlang.org/phobos/std_typecons.html#Nullable void main() { Nullable!int x; // requires: import std.typecons assert(x.isNull); writeln("x is ", x); x = 1; assert(!x.isNull); writeln("x is ", x); x.nullify(); // Forces x back to a null state. assert(x.isNull); writeln("x is ", x); } // ----------------------------------
null != void
Nov 27
next sibling parent codephantom <me noyb.com> writes:
On Tuesday, 28 November 2017 at 05:10:39 UTC, bauss wrote:
 null != void
"initialized or not?" != void
Nov 27
prev sibling parent codephantom <me noyb.com> writes:
On Tuesday, 28 November 2017 at 05:10:39 UTC, bauss wrote:
 null != void
also...void is a completely useless concept for initialisation. what can you determine about the nothingness of void? ... nothing. writeln(typeof(void).stringof); // ?? what do I know now? nothing. vs Nullable!int x; writeln(typeof(x).stringof); // Nullable!int .. now I know something.
Nov 27