Jump to page: 1 2
Thread overview
static class vs. static struct
Jan 26, 2015
ref2401
Jan 26, 2015
bearophile
Jan 26, 2015
Piotrek
Jan 26, 2015
anonymous
Jan 27, 2015
ref2401
Jan 27, 2015
Daniel Kozak
Jan 27, 2015
Daniel Kozak
Jan 27, 2015
ketmar
Jan 27, 2015
Artur Skawina
Jan 27, 2015
Piotrek
Jan 27, 2015
Ali Çehreli
Jan 27, 2015
Piotrek
Jan 27, 2015
Ali Çehreli
Jan 27, 2015
Piotrek
Jan 27, 2015
Ali Çehreli
Jan 27, 2015
Piotrek
Jan 28, 2015
Ali Çehreli
Jul 15, 2015
creiglee
Nov 20, 2015
vyarthrot
January 26, 2015
What's the difference between static class and static struct?
What should i use?
January 26, 2015
On Monday, 26 January 2015 at 14:02:54 UTC, ref2401 wrote:
> What's the difference between static class and static struct?
> What should i use?

Non-static structs/classes have an extra pointer. Static ones don't have it, so their differences are the usual ones: a class is used by reference and they are often on the heap, while a struct is handled by value (or pointer to value). A class has two extra hidden fields.

Bye,
bearophile
January 26, 2015
On Monday, 26 January 2015 at 14:11:32 UTC, bearophile wrote:
> Non-static structs/classes have an extra pointer.
>
> Bye,
> bearophile

Since when structs have an extra pointer? Maybe you are talking about nested structs?


Piotrek
January 26, 2015
On Monday, 26 January 2015 at 21:33:10 UTC, Piotrek wrote:
> On Monday, 26 January 2015 at 14:11:32 UTC, bearophile wrote:
>> Non-static structs/classes have an extra pointer.
>>
>> Bye,
>> bearophile
>
> Since when structs have an extra pointer? Maybe you are talking about nested structs?

Non-static means nested.
January 27, 2015
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?
January 27, 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?

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
January 27, 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);
}
January 27, 2015
On Tue, 27 Jan 2015 09:40:08 +0000, Daniel Kozak wrote:

> import std.stdio;
> import std.conv;
> 
> struct S {
>      @disable this();
> }
> 
> final class C {
> }
> 
> void main() {
>      writeln(C.sizeof);
>      writeln(S.sizeof);
> }

blind guess: vmt with "toString()" from Object? ;-)

January 27, 2015
On Monday, 26 January 2015 at 21:55:19 UTC, anonymous wrote:
> On Monday, 26 January 2015 at 21:33:10 UTC, Piotrek wrote:
>> On Monday, 26 January 2015 at 14:11:32 UTC, bearophile wrote:
>>> Non-static structs/classes have an extra pointer.
>>>
>>> Bye,
>>> bearophile
>>
>> Since when structs have an extra pointer? Maybe you are talking about nested structs?
>
> Non-static means nested.

Hmm,this can be misleading. Nesting in structs doesn't introduce context pointer.

But I agree that if we take into account a hypothetical inferred static attribute for "nesting in struct" and the module scope cases, then the static and non-static classification looks the most suitable.

Piotrek
January 27, 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
« First   ‹ Prev
1 2