Challenge: write a template-instance free dispatcher for text printing
Paul Backus
snarwin at gmail.com
Wed May 27 18:50:08 UTC 2026
On Wednesday, 27 May 2026 at 18:13:40 UTC, H. S. Teoh wrote:
> Although D templates are extremely powerful and awesome for
> metaprogramming, templates are ultimately not the right concept
> for many CT tasks. What we *really* want is CTFE + type
> manipulation for DbI to compute the final form of a template
> instantiation, then a single template instantiation at the end
> to actualize it. The current idiom of arbitrarily-complex
> templates nested arbitrarily deep, while powerful, leads to
> needless template bloat.
What you're describing is Lisp macros, and Walter has gone on
record many, many times that D will never have them.
I think the best we can do is probably something similar to C++'s
{fmt} library, which uses type erasure and runtime dispatch to
avoid unnecessary template instantiations. For example, if we are
formatting 5 different structs that all have toString() methods,
there is no need to instantiate 5 completely separate copies of
writeln--instead, we can have one instance of writeln which makes
an indirect call through an interface/delegate, and 5 instances
of a small helper function which that interface/delegate can
dispatch to.
More information about the Digitalmars-d
mailing list