www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template alias parameter does not accept functions

reply "Dicebot" <m.strashun gmail.com> writes:
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.
Feb 27 2013
next sibling parent "Dicebot" <m.strashun gmail.com> writes:
Actually it looks like a somewhat broken behavior as it works for 
parameter-less functions.
Feb 27 2013
prev sibling parent reply Nick Treleaven <ntrel-public yahoo.co.uk> writes:
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?
Feb 27 2013
parent reply Nick Treleaven <ntrel-public yahoo.co.uk> writes:
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.
Feb 27 2013
parent reply "Dicebot" <m.strashun gmail.com> writes:
Ye, lambda litteral issue is different one.
My code example: http://dpaste.1azy.net/b06370ea
Feb 27 2013
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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); }
Feb 27 2013
parent "Dicebot" <m.strashun gmail.com> writes:
Fuck. Optional. Parens.

Thank you. I have tried some other code than .stringof but guess 
it invoked Symbols somewhere silently, too.
Feb 27 2013
prev sibling parent Nick Treleaven <ntrel-public yahoo.co.uk> writes:
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 }
Feb 27 2013