Multiple alias this, what's the hold up?
Mike Franklin
slavo5150 at yahoo.com
Tue Jun 18 00:01:49 UTC 2019
On Monday, 17 June 2019 at 18:31:18 UTC, Timon Gehr wrote:
> On 17.06.19 15:48, Mike Franklin wrote:
>> void main()
>> {
>> S s;
>> s = 1;
>> int i = s; // No way to make this work in the library.
>> We need `opAssignRight`.
>> }
>
> This is not an assignment. It is an initialization.
> opAssignRight would do nothing for this case.
>
I don't disagree, but that's not what `alias this` does.
```
struct S
{
int i = 42;
alias i this;
}
void main()
{
S s;
int i = s;
writeln("Init: ", i.init, " Value: ", i);
}
```
https://run.dlang.io/is/ra5may
Output:
Init: 0 Value: 42
>> I think if we had an `opAssignRight` feature (with friends) we
>> could move the entire implementation of `alias this` to the
>> library.
>
> I don't think that works. (And I don't necessarily agree that
> it is desirable. See Ethan's excellent post noting issues with
> compile times.)
Sorry, I couldn't find the post. If you explicitly reference it,
I'll read it. If you're saying that implementing `alias this` in
the library would negatively affect compile times, I think you
may be right. But that is a performance bug in the compiler's
implementation. newCTFE and other efforts could help alleviate
that. In general, I thought it was preferred to delegate
features to the library instead of further complicating the
compiler. See https://github.com/dlang/dmd/pull/7988
> This is another case you can't do in the library right now:
>
> struct S{
> int x;
> alias x this;
> }
>
> int foo(int x){
> return x;
> }
>
> void main(){
> import std.stdio;
> writeln(foo(S(3)));
> }
The idea would be for the compiler to lower to something like:
```
int tmp;
s.opAssignRight(tmp); // passed as `ref`
foo(tmp);
```
That's probably still not right, but I think there is a lowering
that would work.
multiple alias this (i.e. DIP66) was approved almost 5 years ago
and despite mutliple attempts at the implementation, it still has
not materialized. The primary blocker is the complexity it
introduced in the compiler
(https://github.com/dlang/dmd/pull/8378#issuecomment-403261763).
Multiple alias this is not going to happen unless it's delegated
to the library. It seems to me that the best way forward is
whatever language feature is missing (`opAssignRight`,
`opImplicitCast`, `opWhatever`) so it can be implemented in the
library, and potentially enable additional idioms in D at the
same time.
Mike
More information about the Digitalmars-d
mailing list