TypeFunction example: ImplictConvTargets

claptrap clap at trap.com
Mon Oct 5 22:12:41 UTC 2020


On Monday, 5 October 2020 at 21:20:15 UTC, Adam D. Ruppe wrote:
> On Monday, 5 October 2020 at 11:44:34 UTC, Stefan Koch wrote:
>> I leave it up to you to decide which version is more 
>> understandable and extendable (should we ever get another 
>> basic type :))
>
> Also trivially easy with mixins. Normally I don't suggest doing 
> things this way but for basic types mixin is guaranteed to work.
>
> ```
>
> enum basic_types = ["bool", "ubyte", "char", "byte", "ushort", 
> "wchar", "short", "uint", "dchar", "int", "ulong", "long"];
>
> // to not import stdlib
> alias AliasSeq(T...) = T;
> string join(string[] s, string j) {
>         string a;
>         foreach(i; s)
>                 a ~= i ~ j;
>         return a;
> }
>
> template basicTypeConvTargets(T) {
>    // again, I copy/pasted your code with very slight 
> modifications
>     string[] helper() {
>             string[] targets;
>             targets.length = basic_types.length;
>             size_t n = 0;
>             static foreach(t;basic_types)
>             {
>                 if (is(T : mixin(t)))
>                 {
>                     targets[n++] = t;
>                 }
>             }
>             return targets[0 .. n];
>     }
>
>     mixin("alias basicTypeConvTargets = AliasSeq!(" ~ 
> helper().join(",") ~ ");");
> }
>
> pragma(msg, basicTypeConvTargets!ushort);

Thats still kinda hideous compared to the TF version. I mean you 
can grep the TF version at first glance, you literally barely 
have to think about it. Your mixin version takes some skipping 
back and forth between the code to work out what its doing.

There's that maxim that your code is read many times more that it 
is written, even by yourself never mind by other people. So a 
piece of code that takes 10 seconds to understand vs 30 seconds 
is a huge win. Faster for people to debug modify, and less likely 
they'll introduce new bugs.

I mean if somebody comes along a year or two down the line to 
modify the TF version and the Mixin version, its not hard to 
imagine which one are they more likely to screw up.

Which makes me wonder what is going on with the people steering D 
that this doesnt seem to have any importance anymore?


More information about the Digitalmars-d mailing list