TypeFunction example: ImplictConvTargets

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Oct 5 12:50:39 UTC 2020


On 10/5/20 7:44 AM, Stefan Koch wrote:
> Hi,
> 
> I've posted an incomplete version of a semantic translation of 
> ImplictConvTargets from a template into a type function.
> 
> After a few rather trivial fixes let me show you what the code looks 
> like now.
[snip]

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 would make for a nice refactoring PR and would provide a better 
baseline for comparison.

Things could be further simplified - I haven't looked in detail but it 
seems to me each integral converts to all integrals that are as large or 
larger. So if we keep a list of integrals sorted by size we could simply 
return slices from it instead of doing filtering.

Anyway, implementation size or difficulty is not the problem with 
ImplicitConversionTargets - it's completeness and correctness. Also the 
appropriate definition in the language, which now is scattered all over 
and probably less precise than it should. ImplicitConversionTargets 
currently doesn't handle alias this, functions and delegates wich 
co/contravariance, probably a few array conversions (I recall Per 
recently added or is about to add one), and some qualified structs.


More information about the Digitalmars-d mailing list