Type functions with std.algorithm

Stefan Koch uplink.coder at googlemail.com
Sun Oct 11 10:19:56 UTC 2020


On Sunday, 11 October 2020 at 06:22:26 UTC, Petar Kirov 
[ZombineDev] wrote:
> On Sunday, 11 October 2020 at 00:46:37 UTC, Stefan Koch wrote:
>> The code below here works with the current talias_master 
>> branch!
>>
>> alias type = __type;
>>
>> type[] makeTypeArray(type[] types ...)
>> {
>>     return types;
>> }
>>
>> import std.algorithm;
>>
>> enum type[] types = makeTypeArray(int, uint, long, ulong);
>> enum size4 = types.filter!((type a) { return a.sizeof == 4; } 
>> );
>> pragma(msg, () {
>>     type[] result = [];
>>     foreach(t;size4)
>>     {
>>         result ~= t;
>>     }
>>     return result;
>> }().tupleof);
>> // outputs: tuple((int), (uint))
>
> Nice!
>
> Is there an issue with `std.array.array`, or you just prefer 
> manually converting the range to an array? Btw, dmd can 
> evaluate ranges at compile-time (e.g. in static foreach), so 
> .tupleof could potentially also do that (convert both ranges 
> and arrays of types to a tuples).

Indeed there is an issue with std.array.array.

It seems to want to convert __type[] into void[] or something.
Which makes some template constraint fail.
__type[] to void[] cannot be implemented because strictly 
speaking the __type[] doesn't hold any data.

I think this can be fixed, by pretending there to be data, 
however than I loose other nice properties such as __type.sizeof 
== 0;

So I will likely need to find a different way.


More information about the Digitalmars-d mailing list