static class vs. static struct

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 27 08:51:01 PST 2015


On 01/27/15 10:40, Daniel Kozak via Digitalmars-d-learn wrote:
> 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);
> }

D's `class` magically adds a level of indirection, so C.sizeof
gives you just the size of the _reference_.

For the true (ie instance/payload) size you'd have to use 

   __traits(classInstanceSize, C)

artur


More information about the Digitalmars-d-learn mailing list