How to use pure in D 2.0
- by James Dean
While playing around with D 2.0 I found the following problem:
Example 1:
pure string[] run1()
{
string[] msg;
msg ~= "Test";
msg ~= "this.";
return msg;
}
This compiles and works as expected.
When I try to wrap the string array in a class I find I can not get this to work:
class TestPure
{
string[] msg;
void addMsg( string s )
{
msg ~= s;
}
};
pure TestPure run2()
{
TestPure t = new TestPure();
t.addMsg("Test");
t.addMsg("this.");
return t;
}
This code will not compile because the addMsg function is impure. I can not make that function pure since it alters the TestPure object.
Am i missing something? Or is this a limitation?
The following does compile:
pure TestPure run3()
{
TestPure t = new TestPure();
t.msg ~= "Test";
t.msg ~= "this.";
return t;
}
Would the ~= operator not been implemented as a impure function of the msg array? How come the compiler does not complain about that in the run1 function?