www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static class vs. static struct

reply "ref2401" <refactor24 gmail.com> writes:
What's the difference between static class and static struct?
What should i use?
Jan 26 2015
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jan 26 2015
parent reply "Piotrek" <none nowhere.pl> writes:
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
Jan 26 2015
parent reply "anonymous" <anonymous example.com> writes:
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.
Jan 26 2015
next sibling parent reply "ref2401" <refactor24 gmail.com> writes:
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?
Jan 27 2015
next sibling parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Jan 27 2015
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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); }
Jan 27 2015
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 27 Jan 2015 09:40:08 +0000, Daniel Kozak wrote:

 import std.stdio;
 import std.conv;
=20
 struct S {
       disable this();
 }
=20
 final class C {
 }
=20
 void main() {
      writeln(C.sizeof);
      writeln(S.sizeof);
 }
blind guess: vmt with "toString()" from Object? ;-)=
Jan 27 2015
prev sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Jan 27 2015
prev sibling parent reply "Piotrek" <no_data no_data.pl> writes:
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
Jan 27 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/27/2015 08:58 AM, Piotrek wrote:

Nice list. :)

 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
Of course that includes static member functions, where the 'this' pointer is removed. Actually, "static opCall" is kind of different because it makes the type itself callable.
 etc.

 3. static if

 static if(compile_time_cond)
 {
    //this section of code will be taken into the binary, used for meta
 programming
 }
Another use of 'static' that means "at compile time": static assert 4. Module initialization and deinitialization: static this shared static this static ~this shared static ~this 5. Module import: static import std.stdio; Ali
Jan 27 2015
parent reply "Piotrek" <none nowhere.pl> writes:
On Tuesday, 27 January 2015 at 18:18:02 UTC, Ali Çehreli wrote:
 On 01/27/2015 08:58 AM, Piotrek wrote:

 Nice list. :)

 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 Of course that includes static member functions, where the 'this' pointer is removed. Actually, "static opCall" is kind of different because it makes the type itself callable.
 etc.

 3. static if

 static if(compile_time_cond)
 {
    //this section of code will be taken into the binary, used
for meta
 programming
 }
Another use of 'static' that means "at compile time": static assert 4. Module initialization and deinitialization: static this shared static this static ~this shared static ~this 5. Module import: static import std.stdio; Ali
Thanks for comments, Mr. Professor. On duty as usual ;) Let me here thank for your book which I've been reading for some time. Piotrek
Jan 27 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/27/2015 01:44 PM, Piotrek wrote:

 Let me here thank for your book
I am glad that it is useful.
 which I've been reading for some time.
Me too! I browsed the index section to remember the other uses of 'static'. :) Ali
Jan 27 2015
prev sibling parent reply "Piotrek" <no_data no_data.pl> writes:
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
Jan 27 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/27/2015 08:33 AM, Piotrek wrote:

 Non-static means nested.
Hmm,this can be misleading. Nesting in structs doesn't introduce context pointer.
You must be thinking of structs nested inside user-defined types. Structs that are nested inside functions do have the context pointer. Ali
Jan 27 2015
parent reply "Piotrek" <none nowhere.pl> writes:
On Tuesday, 27 January 2015 at 18:24:29 UTC, Ali Çehreli wrote:
 On 01/27/2015 08:33 AM, Piotrek wrote:

 Non-static means nested.
Hmm,this can be misleading. Nesting in structs doesn't
introduce context
 pointer.
You must be thinking of structs nested inside user-defined types. Structs that are nested inside functions do have the context pointer. Ali
What you wrote about the structs is true. However I was referring to other thing. I just wanted to emphasize (with my poor English) that also classes and structs *nested in struct* doesn't contain the additional context pointer. As opposed to class nested in class. Then I think we'd better not say that non-static means nested. Piotrek
Jan 27 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/27/2015 01:33 PM, Piotrek wrote:

 On Tuesday, 27 January 2015 at 18:24:29 UTC, Ali Çehreli wrote:
 On 01/27/2015 08:33 AM, Piotrek wrote:

 Non-static means nested.
Hmm,this can be misleading. Nesting in structs doesn't
introduce context
 pointer.
Oh, I misread what you wrote. Sorry...
 classes and structs *nested in struct* doesn't contain the additional
 context pointer. As opposed to class nested in class.

 Then I think we'd better not say that non-static means nested.

 Piotrek
Makes sense. Ali
Jan 27 2015
prev sibling next sibling parent "creiglee" <creiglee yahoo.com> writes:
In simple words, Singleton is a pattern and not a keyword. The 
Singleton pattern has several advantages over static classes. A 
singleton allows a class for which there is just one, persistent 
instance across the lifetime of an application. That means, it 
created a single instance and that instance (reference to that 
instance) can be passed as a parameter to other methods, and 
treated as a normal object. While a static class allows only 
static methods and and you cannot pass static class as parameter. 
More about.....

http://net-informations.com/faq/netfaq/singlestatic.htm

Lee
Jul 15 2015
prev sibling parent vyarthrot <vyarthrot yahoo.com> writes:
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?
In simple words, Singleton is a pattern and not a keyword. The Singleton pattern has several advantages over static classes. A singleton allows a class for which there is just one, persistent instance across the lifetime of an application. That means, it created a single instance and that instance (reference to that instance) can be passed as a parameter to other methods, and treated as a normal object. While a static class allows only static methods and and you cannot pass static class as parameter. Full Source: http://net-informations.com/faq/netfaq/singlestatic.htm Vyar
Nov 19 2015