Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 26, 2015 static class vs. static struct | ||||
---|---|---|---|---|
| ||||
What's the difference between static class and static struct? What should i use? |
January 26, 2015 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Piotrek | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak Attachments: | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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 Re: static class vs. static struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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
|
Copyright © 1999-2021 by the D Language Foundation