Comment #0 by patric.dexheimer — 2016-08-24T13:14:11Z
import std.stdio;
struct Base{
void opAssign(T)(T x){
writeln("assigning : ",x);
}
}
struct Derived{
Base parent;
alias parent this;
}
void main()
{
Base a;
a = 10; //ok
Derived b;
b = 20; //Error
}
Output:
Error: function f937.Derived.opAssign (Derived p) is not callable using argument types (int)
Comment #1 by john.michael.hall — 2018-03-22T19:46:50Z
Structs have opAssign defined by default. Alias this only forwards undefined lookups. Below is a workaround.
import std.stdio : writeln;
struct Base{
void opAssign(T)(T x){
writeln("assigning : ",x);
}
}
struct Derived{
Base parent;
alias parent this;
void opAssign(T)(T x){
parent = x;
}
}
void main()
{
Base a;
a = 10; //ok
Derived b;
b = 20; //Error
}
Comment #2 by schveiguy — 2020-09-04T12:14:51Z
Ran into this today.
If the compiler is going to generate an opAssign, it should generate one that forwards to the alias-this member. It should be something equivalent to adding the following overload to the existing generated opAssign:
auto ref opAssign(T)(auto ref T val) if (!is(T == typeof(this)) && __traits(compiles, aliasThisMember = val))
{
aliasThisMember = val;
}
BTW, thanks for the workaround. For a wrapped single alias-this member, it works perfectly.
Comment #3 by robert.schadek — 2024-12-13T18:49:39Z