Created attachment 1758
Code example
This is one of two issues that I believe have been harassing me all day, night, and early morning with mixins.
Declare a function in an object. Mixin an overload. Try to call it. Compiler error.
Comment #1 by simen.kjaras — 2019-07-24T12:02:00Z
This is by design, and can easily be fixed with an alias:
mixin template Thing( )
{
public void writeAThing( float val )
{
import std.stdio : writeln;
writeln( val );
}
}
struct ProperThing
{
public void writeAThing( int val )
{
import std.stdio : writeln;
writeln( val );
}
// Note that we must give the mixin a name:
mixin Thing!() f;
// Then this line adds it to the overload set:
alias f.writeAThing writeAThing;
}
void main
{
ProperThing thing;
thing.writeAThing( 42.0f );
}
It's even possible to automate this with reflection:
string aliasAll(alias sym)()
{
enum symName = __traits(identifier, sym);
string result = "";
foreach (e; __traits(allMembers, sym))
result ~= "alias "~symName~"."~e~" "~e~";";
return result;
}
And you'd use it like this:
mixin Thing!() f;
mixin(aliasAll!f);
*** This issue has been marked as a duplicate of issue 2157 ***