Bug 20080 – [2.087.0] Function overloads defined in mixin not callable

Status
RESOLVED
Resolution
DUPLICATE
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2019-07-24T02:09:59Z
Last change time
2019-07-24T12:02:00Z
Assigned to
No Owner
Creator
Ethan Watson

Attachments

IDFilenameSummaryContent-TypeSize
1758file_20080.txtCode exampletext/plain422

Comments

Comment #0 by gooberman — 2019-07-24T02:09:59Z
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 ***