www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct vs. Class

reply "Chris" <wendlec tcd.ie> writes:
I have still some classes lying around in my code. As threading 
is becoming more and more of an issue, classes and OOP in general 
turn out to be a nuisance. It's not so hard to turn the classes 
into structs, a lot of classes are in fact singletons (yes, I've 
been kinda fading out classes for a while now). My question is 
now, what do I have to keep in mind if I turn a cached class into 
a cached struct, i.e.

initProgram()
{
   auto myClass = new AwesomeDoEverything.instance();  // lives 
for the rest of the program, is a singleton
}

// ...

   myClass.call(input);

initProgram()
{
   auto myStruct = AwesomeDoEverything();  // lives for the rest 
of the program
}

// ...

   myStruct.call(input);  // Can live in different threads without 
reinitializing it.

Or is there a way I can make a singleton class "thread-able"?

I cannot afford to re-initialize certain classes / structs 
because I don't wanna hit the file system every time I do so.
Jun 26 2015
next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Fri, 26 Jun 2015 11:11:15 +0000
Chris via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 I have still some classes lying around in my code. As threading 
 is becoming more and more of an issue, classes and OOP in general 
 turn out to be a nuisance. It's not so hard to turn the classes 
 into structs, a lot of classes are in fact singletons (yes, I've 
 been kinda fading out classes for a while now). My question is 
 now, what do I have to keep in mind if I turn a cached class into 
 a cached struct, i.e.
 
 initProgram()
 {
    auto myClass = new AwesomeDoEverything.instance();  // lives 
 for the rest of the program, is a singleton
 }
 
 // ...
 
    myClass.call(input);
 
 initProgram()
 {
    auto myStruct = AwesomeDoEverything();  // lives for the rest 
 of the program
 }
 
 // ...
 
    myStruct.call(input);  // Can live in different threads without 
 reinitializing it.
 
 Or is there a way I can make a singleton class "thread-able"?
 
 I cannot afford to re-initialize certain classes / structs 
 because I don't wanna hit the file system every time I do so.
http://dlang.org/library/std/concurrency/init_once.html
Jun 26 2015
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Fri, 26 Jun 2015 11:11:15 +0000
Chris via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 I have still some classes lying around in my code. As threading 
 is becoming more and more of an issue, classes and OOP in general 
 turn out to be a nuisance. It's not so hard to turn the classes 
 into structs, a lot of classes are in fact singletons (yes, I've 
 been kinda fading out classes for a while now). My question is 
 now, what do I have to keep in mind if I turn a cached class into 
 a cached struct, i.e.
 
 initProgram()
 {
    auto myClass = new AwesomeDoEverything.instance();  // lives 
 for the rest of the program, is a singleton
 }
 
 // ...
 
    myClass.call(input);
 
 initProgram()
 {
    auto myStruct = AwesomeDoEverything();  // lives for the rest 
 of the program
 }
 
 // ...
 
    myStruct.call(input);  // Can live in different threads without 
 reinitializing it.
 
 Or is there a way I can make a singleton class "thread-able"?
 
 I cannot afford to re-initialize certain classes / structs 
 because I don't wanna hit the file system every time I do so.
bad link: http://dlang.org/phobos/std_concurrency.html#.initOnce
Jun 26 2015
parent "Chris" <wendlec tcd.ie> writes:
On Friday, 26 June 2015 at 11:28:38 UTC, Daniel Kozák wrote:
 On Fri, 26 Jun 2015 11:11:15 +0000
 Chris via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com> wrote:

 I have still some classes lying around in my code. As 
 threading is becoming more and more of an issue, classes and 
 OOP in general turn out to be a nuisance. It's not so hard to 
 turn the classes into structs, a lot of classes are in fact 
 singletons (yes, I've been kinda fading out classes for a 
 while now). My question is now, what do I have to keep in mind 
 if I turn a cached class into a cached struct, i.e.
 
 initProgram()
 {
    auto myClass = new AwesomeDoEverything.instance();  // lives
 for the rest of the program, is a singleton
 }
 
 // ...
 
    myClass.call(input);
 
 initProgram()
 {
    auto myStruct = AwesomeDoEverything();  // lives for the 
 rest
 of the program
 }
 
 // ...
 
    myStruct.call(input);  // Can live in different threads 
 without
 reinitializing it.
 
 Or is there a way I can make a singleton class "thread-able"?
 
 I cannot afford to re-initialize certain classes / structs 
 because I don't wanna hit the file system every time I do so.
bad link: http://dlang.org/phobos/std_concurrency.html#.initOnce
Thanks a million! This might do the trick and my classes can live happily ever after :-)
Jun 26 2015