Multiple alias this and alias this in classes
Ruby The Roobster
rubytheroobster at yandex.com
Mon Jan 16 23:46:08 UTC 2023
On Monday, 16 January 2023 at 23:27:39 UTC, deadalnix wrote:
> During a recent discussion with several D folks, several
> people, including Walter told ma that the situation with
> multiple alias this and alias this in classes was hopelessly
> broken. I suggested it needed to be either fixed or removed.
>
> However, during this discussion, I took the fact it was
> hopelessly broken for granted. But it doesn't appear to me that
> this is obvious. Can someone familiar with the topic care to
> explain or point me to a link where this case was made already?
>
> On an asside, I implemented both multiple alias this and alias
> this for classes in SDC. Because these feature were never
> completed in DMD, I had to extrapolate to figure out what the
> semantic should be, and this is what I came up with.
>
> 1/ Alias this is used in two contextes: casts and identifier
> resolution.
> 2/ For struct, it just follow the expected rule for cast and
> resolution, if these rule succeed, then nothing more happen. If
> they fail, then the operation is repeated again with all the
> declared alias this. If the number of valid result is 1 and
> exactly 1, then it is used. If the number of valid result is 0
> or greater than 1, then an error is emitted.
> 3/ For classes, lookups ignore alias this and walk up the
> parent stack, still ignoring any alias this. If that fails,
> then all the alias this from this class and its parent are run
> at once, and if the number of valid result is 1, then this is
> fine, if it is 0 or greater than 1, then an error is emitted.
> The same is done for casts.
>
> I think this semantic is reasonable. But I have to say I did
> not put a ton of though into it, so maybe there is a big fatal
> flaw I'm overlooking.
The most general situation for an `alias this` is (assuming no
name conflicts) :
```d
class(T ...) C
{
public:
static foreach(t; T)
{
mixin("alias this t some" ~ t.stringof ~ ";");
}
}
```
Assuming that all of the types in T are unique, then an object of
C can be used as any of those types in T. The reasons why `alias
this` is considered broken are:
1) It's implementation is buggy,
2) Multiple `alias this` hasn't been implemented, and it's been
years.
3) It can be used as a workaround for multiple inheritance,
which most people here hate.
The spec can be found
[here](https://dlang.org/spec/class.html#alias-this).
More information about the Digitalmars-d
mailing list