Yet yet yet yet another thread about const and immutable.

Don nospam at nospam.com
Tue Oct 25 08:27:09 PDT 2011


On 25.10.2011 14:50, Timon Gehr wrote:
> On 10/25/2011 02:23 PM, Jude Young wrote:
>> I'm sure that there is much more, but I immutable is set to global
>> storage and not Thread Local Storage.
>> I believe that const is stil TLS.
>>
>> On Tue, Oct 25, 2011 at 5:15 AM, Gor Gyolchanyan
>> <gor.f.gyolchanyan at gmail.com <mailto:gor.f.gyolchanyan at gmail.com>> wrote:
>>
>> What's the difference between const-declared variable and
>> immutable-declared variable?
>>
>> module a;
>>
>> const(int) a;
>> immutable(int) b;
>>
>> void main()
>> {
>> }
>>
>>
>
> Both are shared:
>
> import std.stdio, std.concurrency;
>
> immutable int x;
> const int y;
>
> void main(){
> auto p=(cast(int*)&x);
> *p = 1;
> p=(cast(int*)&y);
> *p = 2;
> writeln(x, " ", y); // 1 2
> spawn(function{writeln(x, " ", y);}); // 1 2
> }
>
>

Initializers are very important. The distinction is:

const int x;
const int y = 2;

x is a static variable in TLS, it is initialized inside module this().
y is a compile-time constant.
I'm surprised that immutable static variables are in TLS. It doesn't 
seem to be necessary.


More information about the Digitalmars-d mailing list