[Issue 14682] [REG2.037] Incorrect interpretation of ~ []

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jun 12 02:29:14 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14682

--- Comment #12 from Kenji Hara <k.hara.pg at gmail.com> ---
(In reply to Kenji Hara from comment #3)
> At a concatenation expression, arr ~ elem is preferred than arr ~ arr2 when
> elem is implicitly convertible to typeof(arr[i]). In the code, the rhs `[]`
> is implicitly convertible to typeof("foo") == string.

Sorry I was mistaken. Normally D's array concatenation prefers two arrays
concat, rather than prepending/appending one element.

I noticed a comment in CatExp::semantic explains how the ambiguity should be
resolved to.

    if (tb1next && tb2next &&
        (tb1next->implicitConvTo(tb2next) >= MATCHconst ||
         tb2next->implicitConvTo(tb1next) >= MATCHconst
        )
       )
    {
        /* Here to avoid the case of:
         *    void*[] a = [cast(void*)1];
         *    void*[] b = [cast(void*)2];
         *    a ~ b;
         * becoming:
         *    a ~ [cast(void*)b];
         */
    }

And in CatAssignExp::semantic, array appending is already preferred than
element appending.

    int[] a1;
    int[][] a2;
    int[][][] a3;

    import std.stdio;
    { auto x = a1 ~ []; x.writeln(); }  // prints []
    { auto x = a2 ~ []; x.writeln(); }  // prints []
    { auto x = a3 ~ []; x.writeln(); }  // prints []

Therefore I can say it's just an implementation bug in dmd.

--


More information about the Digitalmars-d-bugs mailing list