www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialize to None

reply N.S. <ns null.com> writes:
I'd like to check whether a variable is initialized or not. And 
I'd also like to uninitialize a variable that is already 
initialized. Thanks!

int x = void;

if (x == void)
{
     writeln("x not initialized");
}
else
{
     // OK, use x
}

// Uninitialize x
x = void;
Sep 05 2020
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/5/20 11:42 PM, N.S. wrote:
 I'd like to check whether a variable is initialized or not. And I'd also 
 like to uninitialize a variable that is already initialized. Thanks!
 
 int x = void;
 
 if (x == void)
There isn't a way to check this.
 {
      writeln("x not initialized");
 }
 else
 {
      // OK, use x
 }
 
 // Uninitialize x
 x = void;
 
When you set the initial value of x to void, it means "don't do anything, let whatever data is already on the stack be the value of x". So "uninitialize" doesn't really mean anything. It would technically mean "do nothing". -Steve
Sep 05 2020
parent N.S. <ns null.com> writes:
On Sunday, 6 September 2020 at 03:56:51 UTC, Steven Schveighoffer 
wrote:
 On 9/5/20 11:42 PM, N.S. wrote:
 I'd like to check whether a variable is initialized or not. 
 And I'd also like to uninitialize a variable that is already 
 initialized. Thanks!
 
 int x = void;
 
 if (x == void)
There isn't a way to check this.
 {
      writeln("x not initialized");
 }
 else
 {
      // OK, use x
 }
 
 // Uninitialize x
 x = void;
 
When you set the initial value of x to void, it means "don't do anything, let whatever data is already on the stack be the value of x". So "uninitialize" doesn't really mean anything. It would technically mean "do nothing". -Steve
Do I have to create additional variable bool x_is_valid? Problem is I got many variables. Thanks.
Sep 05 2020
prev sibling parent reply starcanopy <starcanopy protonmail.com> writes:
On Sunday, 6 September 2020 at 03:42:36 UTC, N.S. wrote:
 I'd like to check whether a variable is initialized or not. And 
 I'd also like to uninitialize a variable that is already 
 initialized. Thanks!

 int x = void;

 if (x == void)
 {
     writeln("x not initialized");
 }
 else
 {
     // OK, use x
 }

 // Uninitialize x
 x = void;
You might consider Nullable from std.typecons; however, if you're avoiding implicit initialization for performance reasons, then this solution might not be applicable. import std.stdio: writeln; import std.typecons: Nullable; Nullable!int x; if (x.isNull) { writeln("x not 'initialized'"); } else { // OK, use x } // 'Uninitialize' x x.nullify();
Sep 05 2020
parent N.S. <ns null.com> writes:
On Sunday, 6 September 2020 at 05:06:26 UTC, starcanopy wrote:
 On Sunday, 6 September 2020 at 03:42:36 UTC, N.S. wrote:
 I'd like to check whether a variable is initialized or not. 
 And I'd also like to uninitialize a variable that is already 
 initialized. Thanks!

 int x = void;

 if (x == void)
 {
     writeln("x not initialized");
 }
 else
 {
     // OK, use x
 }

 // Uninitialize x
 x = void;
You might consider Nullable from std.typecons; however, if you're avoiding implicit initialization for performance reasons, then this solution might not be applicable. import std.stdio: writeln; import std.typecons: Nullable; Nullable!int x; if (x.isNull) { writeln("x not 'initialized'"); } else { // OK, use x } // 'Uninitialize' x x.nullify();
Perfect, thanks!
Sep 05 2020