Reimplementing the bulk of std.meta iteratively

Steven Schveighoffer schveiguy at gmail.com
Sat Sep 26 22:00:20 UTC 2020


On 9/26/20 5:42 PM, Andrei Alexandrescu wrote:
> On 9/26/20 5:16 PM, Timon Gehr wrote:
>> On 26.09.20 18:18, Andrei Alexandrescu wrote:
>>>
>>> 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;
>>> }());
>>
>> I am pretty sure this one does not work though.
> 
> Oops, Steve just told me it doesn't have any unittests. So it may as 
> well not be working. Research goes on.

So just to follow up on this, the reason it doesn't work is because the 
relation between a and b must determined BEFORE the call to sort.

In other words, in order for this to work, you have to establish result 
of the lambda for all a and b in the array.

Or optionally, you can save enough information into the "Id" type that 
can be able to enable the comparison at runtime.

The general concept works, but only if you can create a relation 
function that doesn't rely on the types themselves, but on the runtime 
representation of the types.

In other words, such a function would need 3 steps:

1. transform the types into runtime data that is comparable given the types.
2. Run the runtime function that does the transformation
3. Convert the result back to the types.

Such things are not *impossible*, but something like derivative is a 
great example that is really difficult to generate a runtime function 
that can do it.

-Steve


More information about the Digitalmars-d mailing list