Skip to content

Instantly share code, notes, and snippets.

@Biotronic
Created April 29, 2015 22:46
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 Biotronic/ddbcdb0f6206c3f2f147 to your computer and use it in GitHub Desktop.
Save Biotronic/ddbcdb0f6206c3f2f147 to your computer and use it in GitHub Desktop.
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