www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2101] New: Please may I use mutable arrays at compile time?

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

           Summary: Please may I use mutable arrays at compile time?
           Product: D
           Version: 2.013
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: caron800 googlemail.com


The docs say I can't use non-const arrays at compile-time. But I want to, so
this is an enhancement request.

The following does not compile.

    int ctfeTest()
    {
        char[10] a; // <--- PROBLEM
        return 1;
    }

    const int A = ctfeTest();

This does

    int ctfeTest()
    {
        char[] a; // <--- OK
        return 1;
    }

but that's completely pointless. This doesn't:

    int ctfeTest()
    {
        char[] a = new char[10]; // <--- PROBLEM
        return 1;
    }

Nor does this:

    int ctfeTest()
    {
        char[] a;
        a.length = 10; // <--- PROBLEM
        return 1;
    }

Even if we can't have dynamic arrays, what's the harm with static ones?


-- 
May 12 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2101





------- Comment #1 from samukha voliacable.com  2008-05-12 11:41 -------
You could use something like this (quickly hacked, dmd 1.029): 

T[] newCtfeArray(T)(size_t size)
{
    T[] array;
    if (!size)
        return array;

    array ~= T.init;

    if (array.length < size)
        return newCtfeArray(array, size);

    return array;
}

private T[] newCtfeArray(T, U = void)(T[] array, size_t size)
{
    while (array.length * 2 <= size)
        array ~= array;

    if (array.length < size)
        return array ~= newCtfeArray!(T)(size - array.length);

    return array;
}

int[] test()
{
    // Allocate an array of 123 ints. The memory will not be freed, sigh
    auto a = newCtfeArray!(int)(123);
    foreach (i, e; a)
    {
        a[i] = e;        
    }

    return a;
}

void main()
{
    static const a = test();
} 


-- 
May 12 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2101


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #3 from Don <clugdbug yahoo.com.au> 2010-01-10 14:11:14 PST ---
Janice's first case has compiled for quite a long time now. The third case,
involving 'new', is very straightforward.  The final case (changing length) is
doubtful whether it is a good idea. I think with implementing 'new', we can
close this bug.

PATCH to allow dynamic array creation with new. Insert this code into
interpret.c, around line 2690. Also add the declaration for it in NewExp in
expression.h. Also need to change the spec to remove the restriction on using
'new'.

------------
Expression *NewExp::interpret(InterState *istate)
{
#if LOG
    printf("NewExp::interpret() %s\n", toChars());
#endif
    if (newtype->ty == Tarray && arguments && arguments->dim == 1)
    {
    Expression *lenExpr = ((Expression
*)(arguments->data[0]))->interpret(istate);
    if (lenExpr == EXP_CANT_INTERPRET)
        return EXP_CANT_INTERPRET;
    return createBlockDuplicatedArrayLiteral(newtype,
        newtype->defaultInitLiteral(), lenExpr->toInteger());
    }
    error("Cannot interpret %s at compile time", toChars());
    return EXP_CANT_INTERPRET;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 10 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2101


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2010-01-11
22:58:37 PST ---
Changeset 333.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 11 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2101


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2010-01-30
22:40:30 PST ---
fixed dmd 1.056 and 2.040

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