Skip to content

Instantly share code, notes, and snippets.

@Cauterite
Last active August 12, 2016 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cauterite/215bd0c60132d8edfb88220ebeaf6705 to your computer and use it in GitHub Desktop.
Save Cauterite/215bd0c60132d8edfb88220ebeaf6705 to your computer and use it in GitHub Desktop.
roll your own vtables
import std.meta;
import std.traits;
import std.stdio;
class Fooº {
@Dynamic void a() {writeln(`hello from a()`);};
@Dynamic void b() {writeln(`hello from b()`);};
@Dynamic void c() {writeln(`hello from c()`);};
void dispatch(string Name) {
return DispatchTbl[Name](this);
};
private static immutable void function(typeof(this))[string] DispatchTbl;
shared static this() {
writeln(`building dispatch table ...`);
foreach (f; getSymbolsByUDA!(typeof(this), Dynamic)) {
DispatchTbl[__traits(identifier, f)] = Self =>
__traits(getMember, Self, __traits(identifier, f))()
;
};
};
struct Dynamic {};
};
void main() {
writeln(`testing ...`);
string Name = `c`;
(new Fooº).dispatch(Name);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment