Is `alias this` a mistake?

IGotD- nise at nise.com
Thu Aug 5 16:25:43 UTC 2021


On Wednesday, 4 August 2021 at 14:32:17 UTC, bachmeier wrote:
>
> It's difficult to beat the simplicity of alias this. Even if 
> something cam be done with mixin templates, that's a lot of 
> overhead vs `alias x this;`. I can't say I use mixin templates 
> very often, but am skeptical that there's no way to misuse them.

I think that mixin templates beats alias this. You can only have 
one alias this  which I think is a big disadvantage but you can 
have several mixin templates. You can also decide the visibility 
(or scope) for mixin templates. You want your mixin template to 
be visible outwards you just write.

mixin MyTemplate;

If you want to just use your mixin template inside your 
class/struct internally you can do that.

mixin MyTemplate instance;

which also enables several instances of the same mixin template.

mixin MyTemplate instance1;
mixin MyTemplate instance2;


Few D programmers know about this and it is badly documented. 
Also what happens with constructors/deconstructors doesn't seem 
to be documented at all. I'm disappointed with the documentation 
as mixin template is one of the central features of D (or should 
be). If we can point out to people in the documentation that 
alias this will be deprecated and link to mixin templates. Having 
a good tutorial/documentation about mixin templates and I think 
the discussions over alias this will be gone and people will 
adopt mixin templates instead.



More information about the Digitalmars-d mailing list