www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1370] New: Can't const-fold empty array literal

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

           Summary: Can't const-fold empty array literal
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: reiner.pope gmail.com


The following code fails to compile, with error, "non-constant expression 1u ~
[]"


template GetArray(uint i)
{
    static if (i == 0)
        const GetArray = 1u ~ GetArray!(i + 1);
    else
        const uint[] GetArray = [];
}

const uint[] array = GetArray!(0);


-- 
Jul 24 2007
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1370





------- Comment #1 from samukha voliacable.com  2007-07-28 05:31 -------
There is a workaround, if it's of any use.

template GetArray(uint i)
{
    static if (i == 0)
        const GetArray = ([1u] ~ GetArray!(i + 1));
    else
        const uint[] GetArray = [];
}

const uint[] array = GetArray!(0);

----------
One more test case. Fails with error "semicolon expected following auto
declaration, not '~'".

template GetArray(uint i)
{
    static if (i == 0)
        const GetArray = [1u] ~ GetArray!(i + 1); //fails
    else
        const uint[] GetArray = [];
}

const uint[] array = GetArray!(0);


-- 
Jul 28 2007