Simplifying conversion and formatting code in Phobos

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 7 12:49:57 PDT 2016


On 9/7/2016 8:46 AM, Jack Stouffer wrote:
> Yeah, this problem ultimately comes down to healthy use of DRY in Phobos in
> regards to string handling code. This was always the tradeoff with DRY: with
> small pieces of reused code being put into functions, it makes maintenance and
> optimization easier, but code complexity increases. While it can be a bit hard
> to wrap one's head around it, once I did I found that for the most part,
> std.conv is correct in delegating a lot of its functionality to other functions
> in std.conv and std.format in particular in order to stay fast.

Consider a couple pulls I made for dup():

     https://github.com/dlang/druntime/pull/1642
     https://github.com/dlang/druntime/pull/1640
     https://github.com/dlang/druntime/pull/1639

Three templates were removed with no loss in functionality. I'm pretty sure the 
number of dup() templates can be reduced still further.

Consider the pattern of overloads:

    template foo(T) if (condition!T) { }
    template foo(T) if (!condition!T) { }

It makes condition!T a user-facing constraint, which it should not be. A better 
pattern would be:

    template foo(T)
    {
        static if (condition!T)
        {
        }
        else
        {
        }
    }


More information about the Digitalmars-d mailing list