www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ctfe: .idup on static array and static variables

reply Lutger <lutger.blijdestijn gmail.com> writes:
i have two question related to ctfe:

1) returning .idup on local static arrays fail but I don't understand why 
that should be: 

string foo()
{
    char[1] d;
    d[0] = 'd';
    return d.idup;
}

pragma(msg, foo()); // "Error: cannot evaluate foo() at compile time"


.idup is not mentioned in the section on ctfe in the specs. Should this be 
filed as a bug, enhancement request or is it my error?

2) the spec says "expressions in the function may not reference any local 
static variables". However, immutable local static variables do seem to 
work, like this silly example:


string getDigit(int digit)
{
    static immutable(char[10]) digits = "0123456789";
    string result;
    result ~= digits[digit];
    return result;
}

static assert( getDigit(0) == "0" );


Is this ok? 
Dec 13 2009
parent reply Don <nospam nospam.com> writes:
Lutger wrote:
 i have two question related to ctfe:
 
 1) returning .idup on local static arrays fail but I don't understand why 
 that should be: 
 
 string foo()
 {
     char[1] d;
     d[0] = 'd';
     return d.idup;
 }
 
 pragma(msg, foo()); // "Error: cannot evaluate foo() at compile time"
 
 
 .idup is not mentioned in the section on ctfe in the specs. Should this be 
 filed as a bug, enhancement request or is it my error?
Bug.
 2) the spec says "expressions in the function may not reference any local 
 static variables". However, immutable local static variables do seem to 
 work, like this silly example:
 
 
 string getDigit(int digit)
 {
     static immutable(char[10]) digits = "0123456789";
     string result;
     result ~= digits[digit];
     return result;
 }
 
 static assert( getDigit(0) == "0" );
 
 
 Is this ok? 
Yes, that's OK. The spec should probably say it "may not reference any local mutable static variables."
 
 
 
Dec 14 2009
parent Lutger <lutger.blijdestijn gmail.com> writes:
Thanks, I filed this bug for the .idup issue: 
http://d.puremagic.com/issues/show_bug.cgi?id=3615
Dec 14 2009