TypeFunction example creatiing a conversion matrix

Paul Backus snarwin at gmail.com
Fri Oct 2 04:29:00 UTC 2020


On Friday, 2 October 2020 at 03:38:37 UTC, 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.

What you get from typeof(Args[0]) would depend on when it's 
evaluated relative to the other declarations in the template 
body. If it's evaluated before Args[0] is mutated (really, 
re-bound), you get the type of the original arg; if it's 
evaluated after, you get the type of the mapped arg.

Obviously the language spec would have to spell out what the 
order of evaluation is. But that's something it ought to do 
anyway, regardless of this proposal.


More information about the Digitalmars-d mailing list