Self-Modifying code for user settings optimization

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 9 15:43:32 PST 2016


interface IFoo {
	void a();
	void b();
}

__gshared IFoo a, b;
__gshared IFoo instance;

class Foo(bool bar) : IFoo {
	void a() {
		static if (bar) {
			// do something
		} else {
			// do nothing
		}
	}
}

shared static this() {
	a = new Foo!true;
	b = new Foo!false;
}

void update(Lookup lookup) {
	if (lookup["bar"])
		instance = a;
	else
		instance = b;
}

Small indirection when executing to find which function to execute but 
that is the best out of language semantics we have and only works for 
booleans.


More information about the Digitalmars-d mailing list