Template 'this' parameters should work even when the method is static.
For example:
mixin template Constructors(){
this(){ }
this()immutable{ }
this()shared{ }
}
class A {
public:
static C getInstance(this C)() {
return new C();
}
private:
mixin Constructors;
}
class B : A {
private:
mixin Constructors;
}
void main(){
auto a = A.getInstance();
auto b = B.getInstance();
static assert(is(typeof(a)==A));
static assert(is(typeof(b)==B));
auto ai = (immutable(A)).getInstance();
auto bs = (shared(B)).getInstance();
static assert(is(typeof(ai)==immutable(A)));
static assert(is(typeof(bs)==shared(B)));
}
Also see: http://forum.dlang.org/post/[email protected]
Comment #1 by er.krali — 2017-08-03T13:53:46Z
I would add that they should really work pretty much everywhere in a class... i.e. all kind of templates:
---
template isClass(T) {
static if (is(T == class)) {
enum isClass = true;
} else {
enum isClass = false;
}
}
template Super(C) if (isClass!C) {
import std.traits;
import std.meta;
static if (is(C == Object)) {
alias Super = AliasSeq!();
} else {
alias Parents = Filter!(isClass, BaseTypeTuple!C);
static assert (Parents.length == 1);
alias Super = Parents[0];
}
}
class A {
public:
enum desc = "class A";
template getDesc(this C) if (is(C == A)) {
enum getDesc = A.desc;
}
template getDesc(this C) if (!is(C == A)) {
enum getDesc = (Super!C).getDesc ~ " - " ~ C.desc;
}
}
class B : A {
enum desc = "class B";
}
class C : B {
enum desc = "class C";
}
void main() {
static assert(C.getDesc == "class A - class B - class C");
}
Comment #2 by schveiguy — 2017-08-03T17:08:37Z
*** Issue 17714 has been marked as a duplicate of this issue. ***
Comment #3 by nick — 2018-02-01T13:41:05Z
*** This issue has been marked as a duplicate of issue 10488 ***