Self-Modifying code for user settings optimization

Jason Jeffory via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 9 15:58:27 PST 2016


On Saturday, 9 January 2016 at 23:43:32 UTC, Rikki Cattermole 
wrote:
> 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.

I see what you are saying... it should work. Seems like a lot of 
bloat for something relatively trivial. Changing the whole 
context to change a single branch and multiplying the number of 
types might have some long term consequences. Its also 
complexifying the code quite a bit... more prone to errors.

Maybe with a bit of ingenuity these can be overcome.


More information about the Digitalmars-d mailing list