View mode: basic / threaded / horizontal-split · Log in · Help
December 13, 2009
ctfe: .idup on static array and static variables
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?
December 14, 2009
Re: ctfe: .idup on static array and static variables
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."

> 
> 
>
December 14, 2009
Re: ctfe: .idup on static array and static variables
Thanks, I filed this bug for the .idup issue: 
http://d.puremagic.com/issues/show_bug.cgi?id=3615
Top | Discussion index | About this forum | D home