-
-
Save Cauterite/215bd0c60132d8edfb88220ebeaf6705 to your computer and use it in GitHub Desktop.
roll your own vtables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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