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

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Sep 12 11:23:55 PDT 2016


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

--- Comment #2 from Sky Thirteenth <sky.13th.95 at gmail.com> ---
(In reply to ag0aep6g from comment #1)
> 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.

OK, I clearly understand the cost which it may take. But, don't You think that
this "limitation" is weird?

All of possible performance drop it's problem nor of compiler, nor of language,
but of programmer, who wrote this code.

It's already possible to construct complex types on compile time, so why We can
not do that for template functions too?

P.S.: For example, C++ allow this kind of calculations. Yes, in C++ it slow and
ugly looking, but it's not a fault of a concept. It's problem of realization.

--


More information about the Digitalmars-d-bugs mailing list