http://d.puremagic.com/issues/show_bug.cgi?id=2656



--- Comment #6 from Andrei Alexandrescu <and...@metalanguage.com> 2010-03-12 
09:34:50 PST ---
(In reply to comment #5)
> I have a simple suggestion - how about putting this in object.d:
> 
> template octal(string s)
> {
>     static assert(s.length > 0);
>     static assert(s[0] >= '0' && s[0] < '8',
>         "Incorrect character in octal constant: `" ~ s[0] ~ "'");
>     if (s.length == 1)
>     {
>         enum ulong octal = s[0] - '0';
>     }
>     else
>     {
>         enum ulong octal = (s[0] - '0') + 8 * octal!(s[1 .. $]);
>     }
> }
> 
> unittest
> {
>     static assert(octal!"45" == 37);
>     static assert(octal!"0" == 0);
>     static assert(octal!"7" == 7);
>     static assert(octal!"10" == 8);
> }
> 
> Then we can deprecate octal constants by ruling integral literals starting 
> with
> 0 illegal.
> 
> The code above has a few problems, e.g. hardcoding of "ulong" but they can be
> easily solved by using and parsing the customary encodings (U, L, UL) at the
> end of the string.

Meh, I forgot to paste the tested code back from emacs:

template octal(string s)
{
    static assert(s.length > 0);
    static assert(s[0] >= '0' && s[0] < '8',
        "Incorrect character in octal constant: `" ~ s[0] ~ "'");
    static if (s.length == 1)
    {
        enum uint octal = s[0] - '0';
    }
    else
    {
        enum uint octal = 8 * (s[0] - '0') + octal!(s[1 .. $]);
    }
}

unittest
{
    static assert(octal!"45" == 37);
    static assert(octal!"0" == 0);
    static assert(octal!"7" == 7);
    static assert(octal!"10" == 8);
}

For now I changed the type of the constant to uint because that's the most
frequent. Anyway, the message (and the good news) is: we have enough linguistic
means to express octal constants, so we could dispense with the ancient
error-prone notation and not worry about inventing new notation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to