Reimplementing the bulk of std.meta iteratively

claptrap clap at trap.com
Fri Oct 2 03:21:59 UTC 2020


On Thursday, 1 October 2020 at 15:21:40 UTC, Andrei Alexandrescu 
wrote:
> On 10/1/20 2:51 AM, Stefan Koch wrote:
>> On Thursday, 1 October 2020 at 04:08:26 UTC, Walter Bright 
>> wrote:
>>>
>> Here is the correct type function which actually works!
>> 
>> alias[] MostDerived(alias[] types ...)
>> {
>>      sort!((alias a, alias b) => is(a : b))(types);
>>      return types;
>> }
>> 
>> It is Andrei thought would work.
>> With type functions it just works.
>
> From 
> https://gist.github.com/andralex/0d85df38be2d9ffbe89cf1fb51c44213:
>
> alias DerivedToFront(Args...) = dereify!({
>     auto ids = reifyArray!Args;
>     ids.sort;
>     return ids;
> }());

Try explaining that to a newbie who's never used D meta 
programming before.

It's a function that sorts a list of types from most to least 
derived.
You do it with an alias template. Here we've use the shorthand 
for an eponymous alias template. So it sort of looks like a 
regular function call but you need an equals sign after the 
template parentheses.
And the right had side needs to be an expression because its an 
alias declaration not a function declaration.
Oh and you need to wrap the right had side in a call to 
dereify(), you dont need to know why for now, you just do.
And you need to wrap the code you want to execute in an anonymous 
lamba that is called immediately.
And before you can do anything with the args passed to the 
template you need to call reify() on them.

I mean seriously? 30 seconds with a straight face?

(Im not even sure ive described it correctly)

vs a type function...

It's a function that sorts a list of types from most to least 
derived.
You write it just like a regular function but with types.


More information about the Digitalmars-d mailing list