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

selective import in function scope fails to merge overload sets #17923

Open
dlangBugzillaToGithub opened this issue Sep 18, 2019 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Simen Kjaeraas reported this on 2019-09-18T12:37:09Z

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

Description

unittest {
    import std.complex : abs;
    import std.math : abs;
    //  template std.complex.abs cannot deduce function
    // from argument types !()(int)
    auto a = abs(1);
}

Changing the order of imports above makes the code compile.

Using regular imports correctly merges overload sets:

unittest {
    import std.complex;
    import std.math;
    auto a = abs(1);
}

This is likely related to the restrictions on overload sets in function scopes:

unittest {
    void fun() {}
    void fun(int i) {} // declaration fun is already defined
}
@dlangBugzillaToGithub
Copy link
Author

simen.kjaras commented on 2019-09-19T07:26:08Z

Note that even though changing the order of imports makes the above code compile, that's because std.math's abs() implementation ends up being the one available, and calling abs() on e.g. a Complex!float would fail in that case (but would compile in the above example).

This same issue occurs when importing a function that also exists in a parent scope:

import std.stdio;

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, abs;

    auto a = complex(1.0,1.0);
    auto b = 1.0f;

    writeln(abs(a));
    writeln(abs(b));
}


In a non-function scope, the solution is to use an alias:

float abs(float f) {
    return f < 0 ? -f : f;
}

struct S {
    import std.complex : complex, abs;
    alias abs = .abs;

    unittest {
        abs(1);
        abs(complex(1,1));
    }
}


However, this doesn't work inside functions:

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, abs;
    alias abs = .abs; // declaration abs is already defined
}


A workaround exists in using a non-function scope to merge the overload sets:

float abs(float f) {
    return f < 0 ? -f : f;
}

unittest {
    import std.complex : complex, cabs = abs;
    alias abs = MergeOverloads!(cabs, .abs);
    abs(1);
    abs(complex(1,1));
}

template MergeOverloads(T...) {
    static foreach (E; T)
        alias MergeOverloads = E;
}

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