On type functions vs T...
Stefan Koch
uplink.coder at googlemail.com
Tue May 5 18:57:04 UTC 2020
On Tuesday, 5 May 2020 at 12:01:27 UTC, Nick Treleaven wrote:
> On Tuesday, 5 May 2020 at 05:03:21 UTC, Andrej Mitrovic wrote:
>> template FilterInts(Args...)
>> {
>> foreach (T; Args)
>> {
>> static if (is(T == int))
>> FilterInts ~= T; // populate a type tuple
>> }
>> }
>
> Looks good; I think a declaration above the foreach e.g.
> `alias[] FilterInts;` would help to make this clearer.
>
> Allowing appending to an alias[] in a template seems less of a
> weighty feature than type functions. The interface to the
> template and the template result are just normal symbols, i.e.
> template parameter sequences.
>
> This is good for both map and filter, unlike the `T...`
> expansion proposal, which only does map:
> https://github.com/dlang/DIPs/blob/956fe8aac9d68a8c8485bd184916faabb0575228/DIPs/DIP10xx.md
>
> T... expansion can expand more than one sequence at once, but
> sequence appending can also do this with an index from foreach:
>
> alias Values = AliasSeq!(1, 2, 3);
> alias Types = AliasSeq!(int, short, float);
> // alias R = cast(Types)Values...; // a DIP example
> alias[] R;
> foreach (i, e; Values)
> R ~= cast(Types[i])e;
>
> Sequence appending is also more flexible because sometimes you
> need the index to a tuple, not just the element:
>
> enum array = [1,0,1];
> alias[] Selection;
> foreach (i, E; Seq)
> static if (array[i])
> Selection ~= E;
>
> It would also be nice to be able to swap elements of an
> alias[], so we can sort it without generating compile-time
> junk. This might cause implementation complications though, IDK.
templates are NOT THE RIGHT TOOL.
It's a hack.
It's ugly.
It leads to tons of complications.
The implementation of type functions is actually very simple
compared to templates.
That's why I am going that route rather than putting more strain
on the template system
which already over-strained because of the constant misuse as
computational instrument.
More information about the Digitalmars-d
mailing list