How to use pure in D 2.0
Posted
by James Dean
on Stack Overflow
See other posts from Stack Overflow
or by James Dean
Published on 2009-06-17T18:31:54Z
Indexed on
2010/05/27
0:31 UTC
Read the original article
Hit count: 320
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?
© Stack Overflow or respective owner