Reimplementing the bulk of std.meta iteratively
Stefan Koch
uplink.coder at gmail.com
Sun Sep 27 00:10:33 UTC 2020
On Saturday, 26 September 2020 at 21:42:19 UTC, 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.
This example on the other hand works:
---
/+
import std.array;
import std.range;
import std.algorithm;
doesn't work yet but only because the template connstraints need
to be adjusted.
+/
alias alias_array = typeof(((alias T) => __traits(getAttributes,
T))(Byte));
auto sort3(alias less, R)(ref /*alias_array*/ R r)
{
alias tmp;
if (r.length == 1)
return r;
if (r.length == 2)
{
if (less(r[1], r[0]))
{
tmp = r[0];
r[0] = r[1];
r[1] = tmp;
}
}
if (r.length == 3)
{
if (less(r[2], r[1]))
{
tmp = r[1];
r[1] = r[2];
r[2] = tmp;
}
if (less(r[1], r[0]))
{
tmp = r[0];
r[0] = r[1];
r[1] = tmp;
}
}
return r;
}
auto sortBySize(alias_array types ...)
{
types.sort3!((alias a, alias b) => a.sizeof < b.sizeof);
return types;
};
alias Int = int;
alias Ushort = ushort; // we need these aliases because the
parser won't allow us to call a function with a reserved type
identifier.
alias Byte = byte;
pragma(msg, sortBySize(Ushort, Int, Byte)); //Output: [(byte),
(ushort), (int)]
---
More information about the Digitalmars-d
mailing list