Template value inference: closing the UFCS gap for template value parameters (or, "foo".format(bla) as a template instantiation)
FeepingCreature
feepingcreature at gmail.com
Mon Aug 5 06:49:32 UTC 2019
Right now, we can write format("Hello %s", 5), or we can write
"Hello %s".format(5). This is an appealing syntax that also
appears in other languages like Python.
To take advantage of compile-time parameter checking, we can also
pass the format string as a template parameter: format!"Hello
%s"(5). If I'd called with (5, 6), it would have been a compile
error. However, there is no equivalent UFCS syntax for
templated-formatstring format!
Forms like "Hello %s".!format(5) seem obvious, but look ugly.
Luckily, there's an easier way.
With something like format("Hello %s", 5); the types of the
parameters to format are translated into template type arguments;
in other words, this is equivalent to format!int("Hello %s", 5).
Let's call this template type inference.
Since D templates have value parameters, this suggests an
analogous process of template value inference. How would this
look?
void format(string fmt, T...)(fmt, T args);
In other words, we specialize format for the *value* of the
template parameter fmt, and then let IFTI/template inference do
the work of figuring out the appropriate template instantiation,
exactly as before.
With this change, format!"..." would no longer be necessary at
all; all format calls of the form format("format string", args)
would be templated on the string's value, as they already are on
the argument types.
Thoughts?
More information about the Digitalmars-d
mailing list