Reimplementing the bulk of std.meta iteratively

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Sep 26 16:18:27 UTC 2020


I'll start with the punchline: at the link below you'll find an 
alpha-quality reimplementation of std.meta in an iterative manner:

https://gist.github.com/andralex/6212ebf2b38c59c96cf66d0009336318

It's the bulk of std.meta - I stopped when it became obvious doing more 
is just more of the same. Compiles and runs with dmd 2.092 and probably 
others.

The implementation uses throughout a reification/dereification approach. 
To reify a type or alias means to convert it into a value. To dereify a 
value means to turn it back into the type or alias it originated from.

The algorithms follow the following pattern consistently:

1. Reify the compile-time parameters into values
2. Carry processing on these values with the usual algorithms
3. If needed, dereify back the results into compile-time parameters

Not all artifacts need step 3. For example, allSatisfy returns a bool, 
not a type.

For the most part implementation has been a healthy gallop that took a 
couple of hours yesterday and a couple of hours today. Its weakest point 
is it uses .stringof for types, which has issues with local types. 
Hopefully https://github.com/dlang/dmd/pull/11797 could help with that.

Most implementations are a few lines long because they get to leverage 
algorithms as implemented in std. Here's a typical one:

alias MostDerived(Args...) = dereify!({
     auto ids = reify!Args;
     sort!((a, b) => is(dereify!a : dereify!b))(ids);
     return ids;
}());

To sort an AliasSeq with most derived elements first, create a lambda to 
reify the arguments into values, sort the values, and return the sorted 
result. Then call the lambda and use dereify against its result. And 
that is IT.

Commments and ideas for improvements are welcome.


More information about the Digitalmars-d mailing list