static map as a type function

Bruce Carneal bcarneal at
Thu Sep 24 03:49:07 UTC 2020

On Thursday, 24 September 2020 at 03:17:44 UTC, Steven 
Schveighoffer wrote:
> 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.

Yes.  Didn't want to speak to these points first, thanks for 
speaking up Steve, but if I'm channeling Stefan correctly 
template memoization/recursion is pretty close to useless in 
practice.  Maybe there's some magic breakthrough possible but 
from what I've been able to pick up, it's not looking good, even 
theoretically.  Instead we're looking at a never ending series of 
hacks that almost work, sometimes, and break opaquely down the 

> 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.

Boring is great.

More information about the Digitalmars-d mailing list