www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Yet yet yet yet another thread about const and immutable.

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
What's the difference between const-declared variable and
immutable-declared variable?

module a;

const(int) a;
immutable(int) b;

void main()
{
}
Oct 25 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 10/25/2011 12:15 PM, Gor Gyolchanyan wrote:
 What's the difference between const-declared variable and
 immutable-declared variable?

 module a;

 const(int) a;
 immutable(int) b;

 void main()
 {
 }

Both cannot change, but one is typed as const and the other as immutable. You usually don't want to declare const variables that way, because they cannot be passed by reference to immutable even though technically they would be immutable.
Oct 25 2011
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Why don't then change then? const-declared variables would make a lot
more sense to be always placed in mutable memory, so that they could
be used as logical consts.

On Tue, Oct 25, 2011 at 4:35 PM, Timon Gehr <timon.gehr gmx.ch> wrote:
 On 10/25/2011 12:15 PM, Gor Gyolchanyan wrote:
 What's the difference between const-declared variable and
 immutable-declared variable?

 module a;

 const(int) a;
 immutable(int) b;

 void main()
 {
 }

Both cannot change, but one is typed as const and the other as immutable. You usually don't want to declare const variables that way, because they cannot be passed by reference to immutable even though technically they would be immutable.

Oct 25 2011