TypeFunction example creatiing a conversion matrix

Paul Backus snarwin at gmail.com
Fri Oct 2 04:25:20 UTC 2020


On Friday, 2 October 2020 at 02:21:03 UTC, Andrei Alexandrescu 
wrote:
> On 10/1/20 7:10 PM, Paul Backus wrote:
>> On Thursday, 1 October 2020 at 21:34:50 UTC, Adam D. Ruppe 
>> wrote:
>>>
>>> So some facility to turn a ctfe array back into a tuple - the 
>>> dereifiy step basically - with compiler assistance I think 
>>> will be practical.
>> 
>> What if we could just mutate tuples locally?
>> 
>> template staticMap(alias F, Args...) {
>>      static foreach(ref Arg; Args)
>>          Arg = F!Arg;
>>      alias staticMap = Args;
>> }
>
> Interesting - this is akin to D's relaxed purity whereby a pure 
> function can mutate its arguments. I'm trying to wrap my head 
> around it, e.g. is Args considered a private copy of the 
> argument much like a static array passed by value?

Kind of. Args is a private copy, not of the arguments themselves, 
but of a list of *references* to the arguments. (In DMD terms, 
TemplateInstance.tiargs an array of pointers to RootObjects.) So 
mutating it just means re-binding the references to point to new 
things.

> I don't get the interaction. How does mutating the argument 
> affect order of declaration?

You're right; it doesn't matter in this case. It would matter if 
you wanted to write a `staticFold`, though:

template staticFold(alias F, Args...)
     if (Args.length > 0)
{
     static foreach (Arg; Args[1 .. $])
         Args[0] = F!(Init, Arg);
     alias staticFold = Args[0];
}


More information about the Digitalmars-d mailing list