What do you use opDispatch for?

Mike Parker aldacron at gmail.com
Mon Mar 22 22:59:54 PDT 2010

Philippe Sigaud wrote:
> As for you, what are your experiences / projects with opDispatch?
> Philippe

I was toying around with the idea of using it for a quick & dirty logger 
and came up with this:

import std.stdio;
import std.string;

struct Log
	File file;
	this(string filename)
		file.open(filename, "w");
	void opDispatch(string s, string f = __FILE__, uint line = __LINE__, 
S...)(S args)
		file.writeln("[", f, " @ ", line, "][", toupper(s), "]", args);		

And then use it like this:

log.error("This is an error.");
log.info("Wouldn't you like to know something?");
debug log.trace("Hey, this is happening now.");
version(AudioStats) log.audio("Some stats: ", foo, bar);

To me, this is much cleaner than the alternative, which would be 
something like:

log.write("ERROR", "This is an error");

More information about the Digitalmars-d-learn mailing list