state of an object

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 2 00:06:57 PDT 2012


On Monday, July 02, 2012 08:19:52 Namespace wrote:
> Is it possible to figure out how is the state of an object at
> compile time?
> E.g. if the object is null or not:
> 
> class Foo { }
> 
> Foo f;
> 
> static if (is_null(f)) { }

What are you trying to test exactly? Whether f default initializes to null? 
That's as easy as

static if(typeof(f).init is null);

But if what you want to test is what f is initialized to  - such that you can 
get the value if it were

Foo f = initialize();

then no, you can't do that.  D won't let you use any global or static variable 
at compile time in order to avoid subtle errors caused by ordering and 
circular dependencies.

If f were an enum you could, but then it's a manifest constant rather than a 
variable, and you can't currenly initialize classes to anything other than 
null at compile time.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list