type variables

Stefan Koch uplink.coder at googlemail.com
Sun Aug 2 12:31:00 UTC 2020

On Sunday, 2 August 2020 at 02:21:30 UTC, Paul Backus wrote:

>> Pure functional programming is great wrt correctness, it's 
>> working today, but it's not-so-great when it comes to 
>> readability/maintainability.  For starters, composition is a 
>> challenge.  Unwinding recursions in your head is another 
>> challenge.  Debugging is another challenge.
> The main issue with recursion is not that it is difficult to 
> understand or maintain, but that it has poor performance. In 
> order to process an argument list of length N using template 
> recursion, N separate template instantiations are required. 
> Using mutation instead of recursion would reduce the memory 
> required by such templates from O(N) to O(1).
> This is the primary motivation behind proposals like Stefan 
> Koch's type functions and Manu's `...` operator.

While performance is indeed the original motivation,
I come to appreciate being able to use imperative style
in type operations a lot.
It's always good to have the choice.

Take this for example.
int[] iota(int start, int finish)
     int[] result = [];
     result.length = finish - start;
     foreach(i;start .. finish)
         result[i - start] = i;
     return result;

Whereas the recursive function for this,
is something I do not even want to put on here.
Because it's way to complicated for this simple task.

>> Additionally, any template recursions extend the type names in 
>> a very ugly way.  Yes, the extension will give you a unique 
>> (unintelligible ginormous) name but that's about it.  Seems 
>> that we should be able to get a unique mangle without the 
>> garbage, something that a human could read and have a prayer 
>> of understanding while the universe is still warm.
> As far as I'm aware, the problem with ginormous names had 
> nothing to do with template *recursion*. Rather, it resulted 
> from the fact that code making heavy use of templates (for 
> example, UFCS chains of std.algorithm and std.range functions) 
> generated names in which the *same exact* type names were 
> *repeated* many, many times.
> If you know of an example of excessively large symbol names 
> resulting *specifically* from template recursion, I would be 
> interested to hear about it.

There are some examples where, even with mangle compression the 
mangles are still execcsively long.
Large string swtiches are the easy demonstaration here.
Those are not due to recusion but recursion certainly does not 

Because the intermediate templates are created explicitly and 
therefore can't be optimised away in the general(!) case.

More information about the Digitalmars-d mailing list