Bug 16426 – Templated opAssign do not forward on Alias this.

Status
REOPENED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-08-24T13:14:11Z
Last change time
2024-12-13T18:49:39Z
Assigned to
No Owner
Creator
SrMordred
Moved to GitHub: dmd#19177 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19177 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB