static map as a type function
schveiguy at gmail.com
Thu Sep 24 03:17:44 UTC 2020
On 9/23/20 10:42 PM, Paul Backus wrote:
> On Thursday, 24 September 2020 at 02:09:31 UTC, Jackel wrote:
>> It's basically what D is to C++ template metaprogramming.
>> Compare the staticMap implementation with a type function
>> implementation and it's pretty clear which one is more readable and
>> easier to maintain. The current D implementation will also create a
>> bunch of template bloat with numerous instantiations that aren't
>> actually required.
>> StaticMap, and many other templates like it also need a workaround to
>> reduce the number of instances, otherwise they fail. Which isn't as
>> intuitive and not something someone will really know to do.
> On the other hand, if you can fix recursive template bloat somehow
> (tail-call elimination?), you get good performance *and* nice code. It's
> the best of both worlds.
The recursive version is not nice code. It's hard to understand and
difficult to follow. It's main advantage is that because it's
essentially a functional language, and all templates are cached, in
certain cases, you can see a performance gain because the compiler can
avoid actually redoing what it did. In practice, for things like
staticMap, this doesn't ever pan out.
I don't see how you solve the tail recursion thing anyway -- each
template instance cannot rely on the work that has already been done,
because of static if.
The static map type function is a loop over an array. Easy to
understand, easy to write. It's actually boring code. Static map
shouldn't be interesting code at all.
More information about the Digitalmars-d