Make shadowing mixin template names an error (5 years old problem)
monkyyy
crazymonkyyy at gmail.com
Sun Jun 21 20:07:04 UTC 2026
On Sunday, 21 June 2026 at 18:59:24 UTC, mw wrote:
> On Saturday, 9 May 2026 at 12:04:45 UTC, Steven Schveighoffer
> wrote:
>> On Tuesday, 5 May 2026 at 21:47:09 UTC, mw wrote:
>>> Make shadowing mixin template names an error
>>>
>>> https://github.com/dlang/dmd/issues/23060#issuecomment-4374242813
>>
>> Dennis is correct. This is how mixin templates are supposed to
>> work, and if we changed that it would be massively disruptive.
>>
>> I would recommend using string mixins for the behavior you
>> want.
>
> Using string mixin worked.
>
> But string mixin and template mixin have different behavior in
> this case shows the language need to improve.
>
> We can do deprecation slowly.
>
> And at least for now, the compiler should generate a warning
> message.
new attempt, now with classes:
```d
import std;
template innate(T,alias value,discrim...){
T innate=value;
}
template innateclassfriendly(T,discrim...){
T innateclassfriendly;
static this(){
innateclassfriendly= new T(allowedtoconstruct());
}
}
mixin template Singleton(T) {
@disable this();
unittest{
static assert( ! __traits(compiles, new T()),"please let me
break, oo needs you to let less code work ;__;");
}
alias getSingleton=innate!(T,T.init,"singleton");
}
struct A {
mixin Singleton!A;
//this() {} // uncommenting this breaks
int i=5;
}
unittest{
assert(A.getSingleton.i==5);
A.getSingleton.i=3;
}
unittest{
assert(A.getSingleton.i==3);
}
mixin template Singleton2() {
alias THIS=typeof(this);
@disable this();
static assert(is(THIS==struct),"classes dont work with innate
for some reason");
alias getSingleton=innate!(THIS,THIS.init,"singleton");
}
struct B{
mixin Singleton2!();
//this() {} // uncommenting this breaks
float f=13.37;
}
unittest{
assert(B.getSingleton.f.isClose(13.37));
}
struct allowedtoconstruct{}
mixin template Singleton3(){
alias THIS=typeof(this);
alias getSingleton=innateclassfriendly!(THIS,"singleton");
this()(){
static assert(0,"please use .getSingleton, or ()'s");
}
this(allowedtoconstruct){}
static THIS opCall()=>getSingleton;
}
class C{
mixin Singleton3!();
string idk;
bool handhold=true;
}
unittest{
//C ohno=new C();//errors out
C().handhold.writeln;
}
```
More information about the Digitalmars-d
mailing list