Bug 4832 – Functions external to class break immutability
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-09-06T13:37:00Z
Last change time
2011-01-26T18:27:09Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2010-09-06T13:37:14Z
Code:
import std.stdio : writeln;
void foo(ref int x)
{
x = 10;
}
class Bar
{
immutable int x;
this()
{
x = 5;
}
void printMe()
{
writeln(this.x);
}
void change()
{
//~ this.x = 20; // illegal, errors out
}
}
void main()
{
Bar bar = new Bar;
bar.printMe(); // prints 5
//~ bar.x = 10; // illegal, errors out
foo(bar.x); // no error!
bar.printMe(); // prints 10
}
The commented out code would error out at compile time, which is expected. But the function bar() is breaking immutability of x.
A relevant bug is bug 4416 , and my comment: http://d.puremagic.com/issues/show_bug.cgi?id=4416#c1
Comment #1 by andrej.mitrovich — 2010-09-06T13:37:55Z
Sorry, I meant the function foo, not bar.
(In reply to comment #0)
> Code:
>
> import std.stdio : writeln;
>
> void foo(ref int x)
> {
> x = 10;
> }
>
> class Bar
> {
> immutable int x;
>
> this()
> {
> x = 5;
> }
>
> void printMe()
> {
> writeln(this.x);
> }
>
> void change()
> {
> //~ this.x = 20; // illegal, errors out
> }
> }
>
> void main()
> {
> Bar bar = new Bar;
> bar.printMe(); // prints 5
>
> //~ bar.x = 10; // illegal, errors out
>
> foo(bar.x); // no error!
> bar.printMe(); // prints 10
> }
>
> The commented out code would error out at compile time, which is expected. But
> the function bar() is breaking immutability of x.
>
> A relevant bug is bug 4416 , and my comment:
> http://d.puremagic.com/issues/show_bug.cgi?id=4416#c1
Comment #2 by yebblies — 2011-01-26T18:27:09Z
*** This issue has been marked as a duplicate of issue 5493 ***