On type functions vs T...

H. S. Teoh hsteoh at quickfur.ath.cx
Tue May 5 19:34:02 UTC 2020


On Tue, May 05, 2020 at 06:57:04PM +0000, Stefan Koch via Digitalmars-d wrote:
[...]
> templates are  NOT THE RIGHT TOOL.
> It's a hack.
> It's ugly.
> It leads to tons of complications.
> The implementation of type functions is actually very simple compared
> to templates.
> That's why I am going that route rather than putting more strain on
> the template system
> which already over-strained because of the constant misuse as
> computational instrument.

Couldn't agree more!  Templates are supposed to be used for
parametrization over types (and other compile-time values), but they're
not intended for actual *computation*.  Just because they happened to be
Turing-complete (which is something the C++ people discovered after the
fact -- it wasn't designed to be that way), doesn't mean they should
actually be used for computations.

Abusing templates for computations leads to a litany of well-known
symptoms, like exorbitant memory consumption by the compiler,
ridiculously-long emitted symbols (remember the bad ole days before
Rainer implemented symbol backreferences? A single symbol could be
hundreds of KB long), excessively long compile times, and template bloat
in the resulting executable.  Which probably were a big cause of
template haters among the C++ crowd (there are certain minorities of C++
programmers who institute project-wide bans on template use -- some of
them would probably turn away from D just by the mere word "template").

Compile-time computation should be done by a proper tool for the job:
CTFE, in D's case. Thanks to the D compiler's smartness in interleaving
CTFE with AST manipulation, there is no loss of expressive power in
doing so; in fact, moving manipulation of type lists aka tuples
(whatever they're called these days, after all these years of back and
forth we still don't have a good word for them) to CTFE probably makes
it even more powerful, because now more complex manipulations are within
easy grasp without the exorbitant memory / total compile time cost.

tl;dr: I can't wait for newCTFE and typelist/tuple manipulation to land
in master. ;-)


T

-- 
I've been around long enough to have seen an endless parade of magic new techniques du jour, most of which purport to remove the necessity of thought about your programming problem.  In the end they wind up contributing one or two pieces to the collective wisdom, and fade away in the rearview mirror. -- Walter Bright


More information about the Digitalmars-d mailing list