View previous topic :: View next topic |
Author |
Message |
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Tue Nov 09, 2010 7:40 am Post subject: const and immutable |
|
|
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 |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Wed Nov 10, 2010 3:23 am Post subject: |
|
|
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 |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Wed Nov 10, 2010 3:38 am Post subject: |
|
|
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 |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Thu Nov 11, 2010 3:24 am Post subject: |
|
|
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 |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Thu Nov 11, 2010 5:48 am Post subject: |
|
|
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 |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Thu Nov 11, 2010 8:17 am Post subject: |
|
|
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? |
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Fri Nov 12, 2010 2:31 am Post subject: |
|
|
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 |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Fri Nov 12, 2010 2:33 am Post subject: |
|
|
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? |
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 |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Fri Nov 12, 2010 2:44 am Post subject: |
|
|
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 |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Sat Nov 13, 2010 4:56 am Post subject: |
|
|
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 |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Sat Nov 13, 2010 8:08 pm Post subject: |
|
|
So immutables only or best use is for threads - thanks, that is all i want to know |
|
Back to top |
|
|
|