Thread overview
[Issue 18558] Template alias spec incomplete
Mar 05, 2018
Jonathan Marler
Mar 05, 2018
ag0aep6g@gmail.com
Mar 05, 2018
Jonathan Marler
Mar 05, 2018
Jonathan Marler
Jun 06, 2018
Nick Treleaven
March 05, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

--- Comment #1 from Jonathan Marler <johnnymarler@gmail.com> ---
Quick clarification.

Based on the example in the comment above, the following:

Foo!return42

treats the `return42` part as a function call with no arguments instead of passing the `return42` symbol to the Foo template.  Note that this is still true even if return42 takes arguments, it will treat it as an erroneous function call.

--
March 05, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

ag0aep6g@gmail.com changed:

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

--- Comment #2 from ag0aep6g@gmail.com ---
(In reply to Jonathan Marler from comment #1)
> Based on the example in the comment above, the following:
> 
> Foo!return42
> 
> treats the `return42` part as a function call with no arguments instead of passing the `return42` symbol to the Foo template.  Note that this is still true even if return42 takes arguments, it will treat it as an erroneous function call.

No. At that point it's an alias of the function. It gets called inside `Foo`:

----
template Foo(alias x)
{
    enum Foo = x; /* This calls x. */
}
----

--
March 05, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

--- Comment #3 from Jonathan Marler <johnnymarler@gmail.com> ---
> No. At that point it's an alias of the function. It gets called inside `Foo`:

Ah I see you're right.  If I add pragma(msg, typeof(x)) inside the template, it
shows that x is a function (not just a uint). If I change it to:

Foo!(return42())

Then x will actually be a uint.

Going a bit further, it looks like alias parameters do accept function calls, but in that case the function call is evaluated outside the template, as demonstrated by this example:

template Foo(alias x)
{
    pragma(msg, typeof(x));
    enum Foo = x;
}
int return42a() { return 42; }
int return42b() { return 42; }
void main()
{
    auto foo1 = Foo!(return42a());
    auto foo2 = Foo!(return42b());
    auto foo3 = Foo!(42);
}

If you compile this, the template Foo only gets instantiated once meaning that all 3 of these templates are the same.

Another interesting use case, it looks like you can also use expressions, i.e.

auto foo4 = Foo!(40 + 2);

--
March 05, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

--- Comment #4 from Jonathan Marler <johnnymarler@gmail.com> ---
Maybe a better description...

template "alias" parameters can accept "symbols" and "values".  If a symbol is passed to the template, i.e.

MyTemplate!someSymbol

Then the symbol is passed directly to the template without being treated as an expression to evaluate.  Even if the symbol is a function, the function is not called and then passed to the template, the function symbol is passed to it.

However, if an "non-symbol" expression is passed to the template, then the expression is evaluated at "compile time" and the return value passed to it, i.e.

MyTemplate!42
MyTemplate!(40 + 2)

enum x = 40;
MyTemplate!(x + 2)

auto return42() { return 42; }
MyTemplate!(return42())

All these represent the same template instantiation.

--
June 06, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                URL|                            |https://github.com/dlang/dl
                   |                            |ang.org/pull/2382
                 CC|                            |nick@geany.org

--
June 17, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

--- Comment #5 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/dlang/dlang.org

https://github.com/dlang/dlang.org/commit/9c53bdc77ba51f30ca3170aa02484ddd0e9ce3ef Fix Issue 18558 - Template alias spec incomplete

https://github.com/dlang/dlang.org/commit/d2e6f38fe8300cf23ae52c943d9a33a4f9a1448c Merge pull request #2382 from ntrel/alias-val

Fix Issue 18558 - Template alias spec incomplete

--
June 17, 2018
https://issues.dlang.org/show_bug.cgi?id=18558

github-bugzilla@puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--