D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16426 - Templated opAssign do not forward on Alias this.
Summary: Templated opAssign do not forward on Alias this.
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-24 13:14 UTC by SrMordred
Modified: 2024-12-13 18:49 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description SrMordred 2016-08-24 13:14:11 UTC
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 John Hall 2018-03-22 19:46:50 UTC
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 Steven Schveighoffer 2020-09-04 12:14:51 UTC
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 dlangBugzillaToGithub 2024-12-13 18:49:39 UTC
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