Reimplementing the bulk of std.meta iteratively

Atila Neves atila.neves at gmail.com
Thu Oct 8 09:51:15 UTC 2020


On Saturday, 26 September 2020 at 16:18:27 UTC, Andrei 
Alexandrescu wrote:
> 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.

1. This is awesome.
2. I agree with everything said about keeping the core language 
simple and lowering to fundamental constructs. I agree with that 
in general, and it's a lot easier to agree with it in the context 
of D given that it's a large language already.

Lisp got mentioned in this thread as if it's some kind of 
failure, and for the life for me I have no idea why.

Users: dmd has too many bugs!
Also users: please add my bugs, err, feature!


More information about the Digitalmars-d mailing list