I have a professor that wrote all his examples for D templates in shorthand:
T exec(alias f,T)(T t) {
return f(t);
}
I can’t find any examples that explain what this means. Can someone here explain it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a function template, the first set of parens hold the template arguments and the second set holds the function arguments.
http://dlang.org/template.html#function-templates
You could rewrite that as:
At the usage point, if a template member has the same name as the template itself, you don’t have to write it twice. This is called the eponymous trick. http://www.bing.com/search?q=eponymous+trick+d+programming+language&qs=n&form=QBRE&pq=eponymous+trick+d+programming+languag&sc=0-0&sp=-1&sk=
Though most D code I’ve seen uses the shorter format – the long template syntax is pretty rare for functions, classes, or structs, which can do it too: struct Foo(T) { } is a struct template with argument T.
The arguments themselves in this exec template are “alias f”, which is any symbol you decide to pass it, e.g., a function or variable name, and “T”, just any generic type. The repeated T are references to that type.
At the usage point, you’ll most likely see it like this:
The second template argument here is figured out implicitly by the function arguments. This is very common with function templates: a lot of the template arguments are implicit so you rarely see them written out. You might see this referenced in D discussions as “IFTI”, which means “implicit function template instantiation”.