TypeFunction example creatiing a conversion matrix

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Oct 2 04:05:00 UTC 2020


On 10/1/20 11:38 PM, Adam D. Ruppe wrote:
> On Friday, 2 October 2020 at 02:21:03 UTC, Andrei Alexandrescu wrote:
>>> template staticMap(alias F, Args...) {
>>>      static foreach(ref Arg; Args)
>>>          Arg = F!Arg;
>>>      alias staticMap = Args;
>>
>> I don't get the interaction. How does mutating the argument affect 
>> order of declaration?
> 
> Is typeof(Args[0]) in there the original arg or the mapped arg?
> 
> In a normal function, there's a distinct before-and-after that, in 
> theory, doesn't exist outside functions (you can use stuff before they 
> are lexically declared)... but even there, you cannot change the type of 
> an already existing variable.
> 
> This would mean you can. Would certainly be strange.... but might work.

That's a rather large change - ref to aliases and such. Maybe a more 
basic facility such as appending to a tuple is simpler?


template staticMap(alias F, Args...) {
     alias Result = AliasSeq!();
     static foreach(Arg; Args)
         Result ~= F!Arg;
     alias staticMap = Result;
}

Now indeed order of evaluation concerns are rather apparent...

This should work with Filter, Reverse etc. as well. Not with 
DerivedToFront (unless a different algo such as mergesort is used).


More information about the Digitalmars-d mailing list