TypeFunction example: ImplictConvTargets
Paul Backus
snarwin at gmail.com
Mon Oct 5 21:13:09 UTC 2020
On Monday, 5 October 2020 at 20:57:04 UTC, Stefan Koch wrote:
> On Monday, 5 October 2020 at 12:50:39 UTC, Andrei Alexandrescu
> wrote:
>
>> The existing implementation is ancient (e.g. predates
>> std.meta) and certainly deserves a redoing with Filter, which
>> turns it into a 3-liner (untested):
>>
>> alias Integrals = AliasSeq!(byte, ubyte, short, ushort, int,
>> uint, long, ulong, CentTypeList, float, double, real, char,
>> wchar, dchar);
>> alias convertsTo(U) = is(T : U);
>> alias ImplicitConversionTargets(T) = Filter!(convertsTo,
>> Integrals);
>
> This code does not work.
> I don't even need to compile it to see that.
It has some simple mistakes, but the fundamental idea is sound.
Here's a version that actually compiles:
import std.meta;
alias Numerics = AliasSeq!(byte, ubyte, short, ushort, int, uint,
long, ulong, float, double, real, char, wchar, dchar);
enum convertsTo(T, U) = is(T : U);
alias ImplicitConversionTargets(T) =
Filter!(ApplyLeft!(convertsTo, T), Numerics);
// prints: (int, uint, long, ulong, float, double, real, dchar)
pragma(msg, ImplicitConversionTargets!int);
// prints: (float, double, real)
pragma(msg, ImplicitConversionTargets!double);
More information about the Digitalmars-d
mailing list