Created
April 29, 2015 22:46
-
-
Save Biotronic/ddbcdb0f6206c3f2f147 to your computer and use it in GitHub Desktop.
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
final class PolymorphicMonostate(T) if (is(T == class)) { | |
static T _instance; | |
static void Create(T2 : T = T, U...)(U args) if (is(typeof(new T2(args)))) { | |
_instance = new T2(args); | |
} | |
T get() { | |
return _instance; | |
} | |
alias get this; | |
} version (unittest) { | |
class A { | |
this(float f) {} | |
int foo() { return 3; } | |
} | |
class B : A { | |
this(string s) {super(2f);} | |
override int foo() { return 5; } | |
} | |
alias MS = PolymorphicMonostate!A; | |
} unittest { | |
MS.Create!B("Arg!"); | |
auto myms = new MS(); | |
assert(myms.foo() == 5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment