Why do we have transitive const, again?
Peter Alexander
peter.alexander.au at gmail.com
Thu Sep 22 10:36:09 PDT 2011
On 21/09/11 6:15 PM, Mehrdad wrote:
> I can't find the thread, but I remember someone (bearophile?) mentioned
> that the reason we have transitive const is to support purity.
>
> I don't think I understand why this is necessary, though -- could
> someone please explain why we have transitive const, and what problems
> it fixes?
>
> Thanks!
It's mostly for concurrent programming.
If I pass an immutable(T) reference type to another thread then I need
to be guaranteed that the object is entirely immutable.
If it weren't for transitive const/immutable, this would be possible:
class Foo
{
Foo m_ref;
this() { m_ref = this; }
Foo get() immutable { return m_ref; }
}
immutable(Foo) foo = new Foo();
Foo surprise = foo.get(); // un-immutable-ified!
More information about the Digitalmars-d
mailing list