Thread overview
Template alias parameter does not accept functions
Feb 27, 2013
Dicebot
Feb 27, 2013
Dicebot
Feb 27, 2013
Nick Treleaven
Feb 27, 2013
Nick Treleaven
Feb 27, 2013
Dicebot
Feb 27, 2013
Andrej Mitrovic
Feb 27, 2013
Dicebot
Feb 27, 2013
Nick Treleaven
February 27, 2013
Looking here: http://dlang.org/template.html#TemplateAliasParameter

There are no function symbols in the list. And quick check that it won't compile.

Two questions arise:
a) Why so?
b) Is there a workaround?

I have started writing some code with UDAs and do want to pass a function to utility template to process without loosing attribute information, thus the issue.
February 27, 2013
Actually it looks like a somewhat broken behavior as it works for parameter-less functions.
February 27, 2013
On 27/02/2013 15:20, Dicebot wrote:
> Looking here: http://dlang.org/template.html#TemplateAliasParameter
>
> There are no function symbols in the list. And quick check that it won't
> compile.
>
> Two questions arise:
> a) Why so?
> b) Is there a workaround?

This has sometimes come up in the NG, with some support for the feature. See also:

http://d.puremagic.com/issues/show_bug.cgi?id=7308

bearophile shows the workaround - use another template:

template ID(alias a){alias a ID;}
void main(){
     alias ID!(x => x ^^ 2) sqrTemplate;
}

On 27/02/2013 17:17, Dicebot wrote:
> Actually it looks like a somewhat broken behavior as it works for
> parameter-less functions.

I'm not sure what you mean, can you show the code it works for?
February 27, 2013
On 27/02/2013 17:38, Nick Treleaven wrote:
> On 27/02/2013 15:20, Dicebot wrote:
>> Looking here: http://dlang.org/template.html#TemplateAliasParameter
>>
>> There are no function symbols in the list. And quick check that it won't
>> compile.
>>
>> Two questions arise:
>> a) Why so?
>> b) Is there a workaround?

Actually I may have misunderstood your question. Anyway, please show the code that doesn't work.
February 27, 2013
Ye, lambda litteral issue is different one.
My code example: http://dpaste.1azy.net/b06370ea
February 27, 2013
On 2/27/13, Dicebot <m.strashun@gmail.com> wrote:
> Ye, lambda litteral issue is different one.
> My code example: http://dpaste.1azy.net/b06370ea

The problem is not in the alias, it's in the call to .stringof, the compiler attempts to invoke the function passed and then call .stringof on it.

If all you want is the name of the function you can use a trait:

template check(alias Symbol)
{
    enum check = __traits(identifier, Symbol);
}
February 27, 2013
Fuck. Optional. Parens.

Thank you. I have tried some other code than .stringof but guess it invoked Symbols somewhere silently, too.
February 27, 2013
On 27/02/2013 18:06, Dicebot wrote:
> Ye, lambda litteral issue is different one.

OK.

> My code example: http://dpaste.1azy.net/b06370ea

The problem may be related to optional parentheses and stringof, and may be a compiler bug.

You can pass function names as a template alias parameter:

import std.stdio;

int f(int i) {return i^^2;}

template check(alias Symbol)
{
	enum check = Symbol(5); // CTFE
}

void main()
{
    //~ pragma(msg, f.stringof); //error

    writeln(check!f); // 25
}