www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10562] New: Cannot initialize arrays by an element value when the elements are fixed-length arrays

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10562

           Summary: Cannot initialize arrays by an element value when the
                    elements are fixed-length arrays
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: acehreli yahoo.com


--- Comment #0 from Ali Cehreli <acehreli yahoo.com> 2013-07-06 22:05:22 PDT ---
We know that arrays can be initialized by a single element value:

void main()
{
    int value = 1;
    int[2] a = value;
    assert(a == [ 1, 1 ]);
}

The bug is that it does not work when the elements are fixed-length arrays
themselves:

void main()
{
    int[3] value = [ 1, 2, 3 ];
    int[3][2] a = value;  // <-- COMPILATION ERROR
    assert(a == [ [ 1, 2, 3 ], [ 1, 2, 3 ] ]);
}

Error: mismatched array lengths, 6 and 3

On the other hand, the array can be initialized by an element-of-an-element:

void main()
{
    // Note: This is the type of an element of array elements
    int value = 1;
    int[3][2] a = value;
    assert(a == [ [ 1, 1, 1 ], [ 1, 1, 1 ] ]);
}

Is that a feature or perhaps a consequence of the reported bug?

Ali

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 06 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10562


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru


--- Comment #1 from Maxim Fomin <maxim maxim-fomin.ru> 2013-07-07 00:10:45 PDT
---
From the spec: "If a slice operator appears as the lvalue of an assignment
expression, and the type of the rvalue is the same as the element type of the
lvalue, then the lvalue's array contents are set to the rvalue. "

Assuming that single value initialization is a semantic equivalent of slice
expression, the code should work.

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