Yet yet yet yet another thread about const and immutable.

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 25 08:33:01 PDT 2011


Nobody answered my question:
Why isn't const guaranteed to be placed in mutable storage?
It won't take advantages of immutable anyway, so why isn't it taking
advantages of potential logical const?

On Tue, Oct 25, 2011 at 7:27 PM, Don <nospam at nospam.com> wrote:
> 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