(This bug was posted to the Japanese community site. I translate this report.)
This code doesn't work!
-------------------------------------------
auto f(string s)
{
return { return s; };
}
void main()
{
static immutable s = f("aaa")();
static assert(s == "aaa");// Error: static assert (null == "aaa") is false
}
-------------------------------------------
The function fails regardless of the argument when execute the following codes in CTFE:
-------------------------------------------
auto f(string s)
{
return { assert(s); };
}
-------------------------------------------
You can take the following measures to avoid this problem:
-------------------------------------------
auto f(string s)
{
auto _s = s;
return { assert(_s); };
}
-------------------------------------------
Comment #1 by clugdbug — 2010-03-30T11:42:35Z
The workaround does NOT work. It only looks as though it does. See this, for example:
===
string delegate() bug4027(string s1) {
string s = s1;
return { return s; };
}
int food() {
auto a = bug4027("aaa");
auto b = bug4027("bbb");
assert(a() == "aaa"); // fails -- a() is "bbb" !!!
return 1;
}
static assert(food()==1);
----
The root cause is that closures are not yet supported in CTFE. They should generate an error message at compile time.