TypeFunction example: filtering types

Stefan Koch uplink.coder at googlemail.com
Thu Oct 8 11:29:45 UTC 2020


On Thursday, 8 October 2020 at 10:59:23 UTC, Stefan Koch wrote:
> Hi there,
>
> Atlia just wrote of what he thinks first class type should 
> allow him to do:
> https://forum.dlang.org/post/uscehdzicfluyjyspocm@forum.dlang.org
>
> Luckily he does not actually require first class types for this.
> type functions do exactly what he wants.
>
> he wrote:
>
> ----
> import std.algorithm: filter, equal;
>
> type[] types = [int, uint, long, ulong];
> auto size4 = types.filter!(a => a.sizeof == 4);
> assert(equal(size4, [int, uint]));

Note this code should actually compile with type functions in 
theory.
It's only implementation constraints imposed by the structure of 
DMD, and straight bugs in the type-function implementation that 
prevent this from working.

right now DMD is not aware that alias is the common type of all 
types and therefore will be unable to type the array literal.
That should be fixed in a couple weeks, similarly alias.init will 
be amended to be the __emptyType.
Which is also a basic type that is not a type, but is not an 
error.


I think after that the using std.algorithm with a type function 
should actually work.


More information about the Digitalmars-d mailing list