[Issue 16486] Compiler see template alias like a separate type in template function definition

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Sep 11 15:37:57 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16486

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g at gmail.com

--- Comment #1 from ag0aep6g at gmail.com ---
Reduced to the core:

----
struct TestType(T) {}
alias TestAlias(T) = TestType!T;
void testFunction(T)(TestAlias!T arg) {}

void main()
{
    TestAlias!int testObj;
    testFunction(testObj); /* "cannot deduce function from argument types
!()(TestType!int)" */
}
----

I don't think this is supposed to work.

It works when testFunction's parameter is TestType!T, because testObj's type
(TestType!int/TestAlias!int) carries the information that it's an instantiation
of template TestType with argument int.

But the compiler has no simple way of telling how TestType!int relates to
TestAlias!T. For example, TestAlias could be defined like this:

----
template TestAlias(T)
{
    static if (is(T == float)) alias TestAlias = TestType!int;
    else alias TestAlias = string;
}
----

With that, testFunction(TestType!int.init) would have to result in T = float.
And with testFunction("") the compiler would have multiple choices for T.

With the original TestAlias it's more straight-forward, but the compiler would
still have to analyze TestAlias's implementation. That's probably not feasible
(maybe impossible) in the general case.

Maybe simple cases could be supported. But that would be an enhancement
request, I think. And then there's always the question of where to draw the
line, and if it's not better to minimize surprises by just rejecting any such
code.

--


More information about the Digitalmars-d-bugs mailing list