Static Associative Array

Jonathan M Davis jmdavisProg at gmx.com
Sun Mar 6 18:27:19 PST 2011


On Sunday 06 March 2011 18:22:52 Peter Lundgren wrote:
> == Quote from Jonathan M Davis (jmdavisProg at gmx.com)'s article
> 
> > On Sunday 06 March 2011 14:05:04 Peter Lundgren wrote:
> > > Can you define an associative array in a way that can be evaluated at
> > > compile time like you can with non-associative arrays?
> > 
> > I'm pretty sure not. I think that it's currently suffering the same fate
> > as stuff like classes and is not yet able to be CTFEed. Some day...
> > - Jonathan M Davis
> 
> If not, then what is the D way to initialize a static field of a struct or
> class a la Java's static initializer blocks? I don't mind constructing the
> associative array at run-time if I have to, but I can't afford to do it
> more than the once needed.

If you can initialize a field with CTFE, then you can do it directly. Otherwise, 
you have to do it with a constructor at runtime. If it's a member field of a 
struct or class, then you use a normal constructor.

class C
{
	this() { ... }
}


If it's a static field of a struct or class, then you use a static constructor in 
that struct or class.

class C
{
    static this() { ... }
}

If it's a variable at the module level, then you use a module constructor (also 
labeled with static).

static this() { ... }

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list