Issue 20080 - [2.087.0] Function overloads defined in mixin not callable
Summary: [2.087.0] Function overloads defined in mixin not callable
Status: RESOLVED DUPLICATE of issue 2157
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 blocker
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-07-24 02:09 UTC by Ethan Watson
Modified: 2019-07-24 12:02 UTC (History)
1 user (show)

See Also:


Attachments
Code example (422 bytes, text/plain)
2019-07-24 02:09 UTC, Ethan Watson
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Ethan Watson 2019-07-24 02:09:59 UTC
Created attachment 1758 [details]
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 Simen Kjaeraas 2019-07-24 12:02:00 UTC
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 ***