TypeFunction example: ImplictConvTargets
Steven Schveighoffer
schveiguy at gmail.com
Tue Oct 6 18:30:15 UTC 2020
On 10/6/20 2:09 PM, Adam D. Ruppe wrote:
> Filter's guts can be:
>
> size_t[Args.length] keep;
> size_t pos = 0;
> foreach(idx, alias arg; Args)
> if(Pred!arg) keep[pos++] = idx; // note idx, not arg.
> return makeResult(keep[0 .. pos]);
>
> In today's D. Again, *almost identical* to the typefunction version,
> just using an index into the list instead of storing the alias in the
> array directly.
I think Filter is a much more apt example than the implicit targets.
Not just because you can make it faster, but because with type
functions, you should be able to actually *use filter*:
return Args.filter!Pred.array;
I mean, we have filter for a reason. You can easily write filter out
into a loop, instead of using filter. But there it is, and it makes
coding so much more pleasant. If the compiler and CTFE can be fast
enough to make this pleasant (I'm not 100% convinced, but it looks
promising), then I'm on board.
It comes down to one thing -- arrays vs. tuples. In type functions, a
tuple is an array, and you can do all the things you can do with a
normal array: mutate, sort, shrink, grow, loop, use as a range, etc.
With a Tuple, everything is immutable, and each change needs to go
across a new template boundary. Even a loop is not really a loop.
That being said, I think the only way type functions make a difference
is if they perform WELL. Even if they perform *as well* as templates
(though indications are they perform better), I'd rather write code in
an imperative style than recursive.
-Steve
More information about the Digitalmars-d
mailing list