www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Detect if variable defined

reply Joseph <JE342 gmail.com> writes:
static if ()
{
  enum x;
}

static if (isDefined!x)
{
}

What's the correct way to check if a variable has been defined? 
(note x may or may not be defined above. I need to know if it is)
Sep 29 2017
next sibling parent arturg <var.spool.mail700 gmail.com> writes:
On Friday, 29 September 2017 at 18:03:52 UTC, Joseph wrote:
 static if ()
 {
  enum x;
 }

 static if (isDefined!x)
 {
 }

 What's the correct way to check if a variable has been defined? 
 (note x may or may not be defined above. I need to know if it 
 is)
import std.traits; static if(hasMember!(T, "x")) { } or if at module level static if(__traits(hasMember, mixin(__MODULE__), "x")) { }
Sep 29 2017
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/29/17 2:03 PM, Joseph wrote:
 static if ()
 {
   enum x;
 }
 
 static if (isDefined!x)
 {
 }
 
 What's the correct way to check if a variable has been defined? (note x 
 may or may not be defined above. I need to know if it is)
Check to see that it has a type: static if(is(typeof(x))) { } -Steve
Sep 29 2017