static class vs. static struct

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 27 01:40:08 PST 2015


On Tuesday, 27 January 2015 at 09:36:49 UTC, Daniel Kozak wrote:
> 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?
>
> I do not think this is a singleton pattern (no instance). I see 
> it much more like namespace in case of core.runtime.Runtime. 
> And yes static final class could do that too but struct looks 
> better than final class and you can disable this on structs

import std.stdio;
import std.conv;

struct S
{
     @disable this();
}

final class C
{
}

void main() {
     writeln(C.sizeof);
     writeln(S.sizeof);
}


More information about the Digitalmars-d-learn mailing list