Last active
March 30, 2016 05:14
-
-
Save alphaKAI/28b6a62fa944988e4f1e to your computer and use it in GitHub Desktop.
Anonymous Class for D Language
This file contains hidden or 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.algorithm, | |
std.typecons, | |
std.string, | |
std.array, | |
std.range, | |
std.conv; | |
template AnonymousClassImpl( | |
string BaseClassName, | |
string bodyString, | |
alias arguments = tuple() | |
) { | |
enum AnonymousClassImpl = () { | |
string generateString( | |
string parameters = null, | |
string argumentLabels = null, | |
string argumentValues = null | |
) { | |
if ( | |
parameters !is null | |
&& argumentLabels !is null | |
&& argumentValues !is null) { | |
return | |
"() {" | |
~ "class AnonymousClassMain : " ~ BaseClassName ~ "{" | |
~ "this(" ~ parameters ~ ") {" | |
~ "super(" ~ argumentLabels ~ ");" | |
~ "}" | |
~ bodyString | |
~ "}" | |
~ "return new AnonymousClassMain(" ~ argumentValues ~ ");" | |
~ "}()"; | |
} else { | |
return | |
"() {" | |
~ "class AnonymousClassMain : " ~ BaseClassName ~ "{" | |
~ "this() {" | |
~ "super();" | |
~ "}" | |
~ bodyString | |
~ "}" | |
~ "return new AnonymousClassMain();" | |
~ "}()"; | |
} | |
} | |
static if (arguments.length == 0) { | |
return generateString; | |
} else { | |
immutable types = (temp) { | |
int i; | |
foreach (argument; arguments.Types) { | |
temp ~= (i ? "," : "") ~ argument.stringof; | |
i++; | |
} | |
return temp; | |
}("").split(","); | |
immutable argumentLabels = arguments.length.iota.map!(i => " arg" ~ i.to!string).array; | |
immutable argumentValues = arguments.stringof.replace("Tuple", ""); | |
immutable parameters = | |
(temp => | |
(Args) { | |
int i; | |
foreach (argument; arguments) { | |
temp ~= (i ? "," : "") ~ types[i].to!string ~ Args[i]; | |
++i; | |
} | |
return temp; | |
}(argumentLabels) | |
)(""); | |
return generateString( | |
parameters, | |
argumentLabels.join(","), | |
argumentValues | |
); | |
} | |
}(); | |
} | |
auto AnonymousClass( | |
string BaseClassName, | |
string bodyString, | |
alias arguments = tuple() | |
)() { | |
static if (arguments.length == 0) { | |
return mixin(AnonymousClassImpl!(BaseClassName, bodyString)); | |
} else { | |
return mixin(AnonymousClassImpl!(BaseClassName, bodyString, arguments)); | |
} | |
} | |
import std.stdio; | |
class T1 { | |
this(string v) { | |
writeln("T1! - ", v); | |
} | |
void func() {} | |
} | |
class Tx1 { | |
this() { | |
writeln("Tx1"); | |
} | |
void func() {} | |
} | |
void main() { | |
T1 t = AnonymousClass!("T1", q{ | |
override void func() { | |
writeln("Overrided T2.func!"); | |
} | |
}, | |
tuple("T2")); | |
Tx1 tx = AnonymousClass!("Tx1", q{ | |
override void func() { | |
writeln("Overrided Tx2.func!"); | |
} | |
}); | |
t.func; | |
tx.func; | |
} |
あ... Dにも無名Classあったのか... 知りませんでした...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
となにか違いますか?