FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const and immutable

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Tue Nov 09, 2010 7:40 am    Post subject: const and immutable Reply with quote

Why D has both? I experience more oder less with both and i doesn't figure out, why D has two keywords for the same thing. I read a lot in the manual but i couldn't find an answer which could explain me exactly, where the difference between immutable and const is. It is possible that anyone post code or explain me the sense and difference between these keywords?
That would be very great.

thanks for regard of my bad english
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Wed Nov 10, 2010 3:23 am    Post subject: Reply with quote

const means basically read only view. Meaning if you have a const variable, for example in a function, you cannot change that value in that function but someone else can change the value outside that function.

immutable means that no one can change the value, you can place immutable in ROM (read only memory). As far as the compiler is concerned, once a immutable value is initialize it's written in stone.
Back to top
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Wed Nov 10, 2010 3:38 am    Post subject: Reply with quote

Code:

int test_immu(int foo) {
    immutable int test = foo;

    return test;
}

int test_const(int bar) {
    const int test = bar;

    return test;
}


you mean something like this? I tried to change the return with

Code:
writeln(test_immu(42) + 1);
writeln(test_const(42) + 2);


and it works fine.

Or did you mean something else?
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Thu Nov 11, 2010 3:24 am    Post subject: Reply with quote

In that case you wouldn't be able to change "test" in "test_immu/const". But since you return "int" which is mutable you can change it outside. BTW, your call to "writeln" doesn't change the return value, it just creates a new value. And this is not a good example anyway because int is a value type so everything will be copied. This is not a very good example but anyway:

Code:
import std.stdio;

void main ()
{
    int a = 1;
   
    immutable int b = 1;
    immutable int* c = &b;
    //b = 2 //cannot change b
    //*c = 7 //cannot change c
    const int* d = &a;
    //*d = 3; // cannot change d
    a = 4; // but I change what d points to via another variable
    writeln(*d); // prints 4
}
Back to top
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Thu Nov 11, 2010 5:48 am    Post subject: Reply with quote

But if i retype a in const int a = 1; and try it now, i earn the same result as with immutable.
The only different i see is, that i can give a normal int to a const, but not to a immutable. is that correct oder exist more differents?
Back to top
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Thu Nov 11, 2010 8:17 am    Post subject: Reply with quote

Code:

    int a = 1;

    immutable int * x = cast(immutable) &a;

    a = 42;

    writefln("X ist: %d", *x); // is 42

I try this and it looks like this works fine. Is that a feature? Wink
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Fri Nov 12, 2010 2:31 am    Post subject: Reply with quote

mutable wrote:
But if i retype a in const int a = 1; and try it now, i earn the same result as with immutable.
The only different i see is, that i can give a normal int to a const, but not to a immutable. is that correct oder exist more differents?


With value types cont and immutable is basically the same. This is the same:

Code:
const int a = 1;
immutable int b = 1;


It's easier to find differences when you're using types with references semantics.
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Fri Nov 12, 2010 2:33 am    Post subject: Reply with quote

mutable wrote:
Code:

    int a = 1;

    immutable int * x = cast(immutable) &a;

    a = 42;

    writefln("X ist: %d", *x); // is 42

I try this and it looks like this works fine. Is that a feature? Wink


You're bypassing the the type system with the cast which results in undefined behavior. If you do that, it's up to you to make sure that "a", in this case, never changes.
Back to top
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Fri Nov 12, 2010 2:44 am    Post subject: Reply with quote

You mean i should try it with class references?
First of all, i would understand, what exactly the difference of immutable and const is, when i have to use immutable and when const or which pros i have if i use immutable instead of const or rather if i use const instead of immutable.
Back to top
View user's profile Send private message
doob



Joined: 06 Jan 2007
Posts: 367

PostPosted: Sat Nov 13, 2010 4:56 am    Post subject: Reply with quote

I don't know how to explain it better than if you have a immutable variable no one can change the value in the variable. If you have a const variable you cannot change the value of the variable but someone else could.

If you use immutable you can share the variable between threads and don't have to worry about synchronization. Since no one can change the value no synchronizations are needed.
Back to top
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Sat Nov 13, 2010 8:08 pm    Post subject: Reply with quote

So immutables only or best use is for threads - thanks, that is all i want to know Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group