Theoretical Best Practices

DarthCthulhu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 14 02:21:55 PDT 2015


This is more a theoretical exercise than specific code nitty 
gritty, but...

Let's say I have some code like this:

class World {

	// Bunch of members and other functions

	void happyDay () {

		if (bCthulhuRises) {

			debug(logging) {

				logger.writeLog(this); // uses the class toString to give a 
dump of interesting parts of the object
			}

			throw new Exception("We're doomed! Run you fools!");

		}

	}

}

I only want to access the logger object when the program is 
compiled with the -debug option, so I don't want to pass it along 
to the object constructor or set a member as a reference to it 
(which is both tedious and pointless if not in -debug mode). The 
simple solution is to make the Logger class a singleton (can D do 
singletons? I presume it's possible, but I haven't really looked 
into it), but is there a means in D that is considered a better 
way to do this?


More information about the Digitalmars-d-learn mailing list