Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templated opAssign do not forward on Alias this. #19177

Open
dlangBugzillaToGithub opened this issue Aug 24, 2016 · 2 comments
Open

Templated opAssign do not forward on Alias this. #19177

dlangBugzillaToGithub opened this issue Aug 24, 2016 · 2 comments

Comments

@dlangBugzillaToGithub
Copy link

SrMordred reported this on 2016-08-24T13:14:11Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=16426

CC List

  • John Hall
  • RazvanN
  • Steven Schveighoffer (@schveiguy)

Description

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)
@dlangBugzillaToGithub
Copy link
Author

john.michael.hall commented on 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
}

@dlangBugzillaToGithub
Copy link
Author

schveiguy (@schveiguy) commented on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant