Template Usage with Eponymous Trick
ShadoLight
ettienne.gilbert at gmail.com
Thu Jan 30 14:10:38 UTC 2020
Taking this example from documentation page on 'Template Sequence
Parameters' [1]:
import std.stdio;
template print(args...)
{
void print()
{
writeln("args are ", args); // args is a ValueSeq
}
}
template write(Args...)
{
void write(Args args) // Args is a TypeSeq
// args is a ValueSeq
{
writeln("args are ", args);
}
}
void main()
{
print!(1,'a',6.8).print(); // prints: args
are 1a6.8
write!(int, char, double).write(1, 'a', 6.8); // prints: args
are 1a6.8
}
This fails to compile with:
onlineapp.d(22): Error: template onlineapp.print cannot deduce
function from argument types !()(void), candidates are:
onlineapp.d(3): print(args...)()
onlineapp.d(23): Error: function onlineapp.write!(int, char,
double).write(int _param_0, char _param_1, double _param_2) is
not callable using argument types ()
onlineapp.d(23): missing argument for parameter #1: int
_param_0
Fixing the error is simply to use 'simplified' template calling
convention for templates based on the 'Eponymous Trick':
print!(1,'a',6.8)(); // prints: args are
1a6.8
write!(int, char, double)(1, 'a', 6.8); // prints: args are
1a6.8
Why does the 'classical' template calling convention not work
anymore in this case? (if the template name and function name are
different it obviously still works). Note the templates were not
defined in the simplified 'Eponymous Trick' style i.e.:
void print(args...)()
{
writeln("args are ", args); // args is a ValueSeq
}
void write(Args...)(Args args) // Args is a TypeSeq
// args is a ValueSeq
{
writeln("args are ", args);
}
...but in the 'classical' default template style, so I would have
thought the
template_name!(compile_time_args).function_name(run_time_args)
style would still work, even if the template and function names
are identical.
If this is in fact now the intended behavior, then there are some
places where the documentation are in error.
[1]: https://dlang.org/spec/template.html#variadic-templates
More information about the Digitalmars-d-learn
mailing list