TypeFunction example: ImplictConvTargets

Stefan Koch uplink.coder at googlemail.com
Mon Oct 5 11:44:34 UTC 2020


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.

---
alias type = alias;

// needed to avoid deeper changes ... in the future it may be 
unnecessary.
auto makeAliasArray(type[] types ...)
{
     return types;
}

enum basic_types = makeAliasArray(bool, ubyte, char, byte, 
ushort, wchar, short, uint, dchar, int, ulong, long);

type[] convTargets(type T)
{
     if (isBasicType(T))
         return basicTypeConvTargets(T);
     return null;
}

bool isBasicType(type T)
{
     foreach(t;basic_types)
     {
         if (is(T == t))
             return true;
     }
     return false;
}

type[] basicTypeConvTargets(type T)
{
     type[] targets;
     targets.length = basic_types.length;
     assert(isBasicType(T), "You may not call this function when 
you don't have a basic type ... (given: " ~ T.stringof ~ ")");
     size_t n = 0;
     foreach(t;basic_types)
     {
         if (is(T : t))
         {
             targets[n++] = t;
         }
     }
     return targets[0 .. n];
}
// 42 lines including whitespace and comments

pragma(msg, convTargets(long)); // outputs [(ulong), (long)]
---

And again here is the part of the template that we just 
re-implemented:
---
     static if (is(T == bool))
         alias ImplicitConversionTargets =
             AliasSeq!(byte, ubyte, short, ushort, int, uint, 
long, ulong, CentTypeList,
                        float, double, real, char, wchar, dchar);
     else static if (is(T == byte))
         alias ImplicitConversionTargets =
             AliasSeq!(short, ushort, int, uint, long, ulong, 
CentTypeList,
                        float, double, real, char, wchar, dchar);
     else static if (is(T == ubyte))
         alias ImplicitConversionTargets =
             AliasSeq!(short, ushort, int, uint, long, ulong, 
CentTypeList,
                        float, double, real, char, wchar, dchar);
     else static if (is(T == short))
         alias ImplicitConversionTargets =
             AliasSeq!(int, uint, long, ulong, CentTypeList, 
float, double, real);
     else static if (is(T == ushort))
         alias ImplicitConversionTargets =
             AliasSeq!(int, uint, long, ulong, CentTypeList, 
float, double, real);
     else static if (is(T == int))
         alias ImplicitConversionTargets =
             AliasSeq!(long, ulong, CentTypeList, float, double, 
real);
     else static if (is(T == uint))
         alias ImplicitConversionTargets =
             AliasSeq!(long, ulong, CentTypeList, float, double, 
real);
     else static if (is(T == long))
         alias ImplicitConversionTargets = AliasSeq!(float, 
double, real);
     else static if (is(T == ulong))
         alias ImplicitConversionTargets = AliasSeq!(float, 
double, real);
     // part omitted because we don't have ucent and cent in our 
list
     else static if (is(T == char))
         alias ImplicitConversionTargets =
             AliasSeq!(wchar, dchar, byte, ubyte, short, ushort,
                        int, uint, long, ulong, CentTypeList, 
float, double, real);
     else static if (is(T == wchar))
         alias ImplicitConversionTargets =
             AliasSeq!(dchar, short, ushort, int, uint, long, 
ulong, CentTypeList,
                        float, double, real);
     else static if (is(T == dchar))
         alias ImplicitConversionTargets =
             AliasSeq!(int, uint, long, ulong, CentTypeList, 
float, double, real);
     // 41 lines including white-space and comments (only that 
there is no white-space or comments)
---

I leave it up to you to decide which version is more 
understandable and extendable (should we ever get another basic 
type :))

As noted previously please do discuss!
Maybe you like the template version more?
Let me know.


More information about the Digitalmars-d mailing list