static class vs. static struct

Piotrek via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 27 08:58:26 PST 2015


On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote:
> For several times I've met struct(or static struct) usage in 
> Phobos for singleton pattern implementation. Unfortunately now 
> i can remember only core.runtime.Runtime.
> So I've got a question. Why do Phobos guys use struct or static 
> struct for or singleton pattern implementation? Why don't use 
> static final class for this purpose?

You probably saw static member function.

Please take the following with a big grain of salt as I took it 
out of my head:
We can divide the D static keyword usage into 3 types:

1. static variable

struct A{int a} // no static before declaration
static A s; //note that static is used for struct variable 
storage class (lifetime)

static int b;
etc.

2. static declaration

static struct A{int a}; //static used for context unnesting
static int fun(){}; // static used also for removing scope context

etc.

3. static if

static if(compile_time_cond)
{
   //this section of code will be taken into the binary, used for 
meta programming
}

I don't think there is much (if any) use of static (type 1) for 
singleton.

Piotrek


More information about the Digitalmars-d-learn mailing list