Why do we have transitive const, again?

Mehrdad wfunction at hotmail.com
Fri Sep 23 11:21:31 PDT 2011


Er, you answered a question about const with an answer about immutable. :\

My point is, what in the world does transitive const have to do with 
transitive immutable?
Can't you have immutable(T) be transitive while const(T) being "normal", 
as in C/C++? If not, why not?

On 9/22/2011 10:36 AM, Peter Alexander wrote:
> 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