digitalmars.D.learn - const vs immutable for local variables
- Jonathan M Davis (11/11) Nov 17 2010 In C++, I tend to declare all local variables const when I know that the...
- bearophile (5/15) Nov 17 2010 Or is dmd dumb enough that it makes no optimization difference? :-)
- Jonathan M Davis (8/28) Nov 17 2010 Well. yes. enums are definitely tha case for compile time constants. The...
- Russel Winder (34/40) Nov 18 2010 question=20
- div0 (6/31) Nov 18 2010 invariant is the old deprecated name for immutable.
- spir (12/15) Nov 18 2010 For me, this is the default as well, meaning locals are constant. I woul...
- Jonathan M Davis (16/49) Nov 18 2010 enums _must_ known at compile time. immutable variables don't, so they w...
- Russel Winder (31/49) Nov 18 2010 n't=20
- Steven Schveighoffer (21/30) Nov 18 2010 immutable and const storage classes are identical as far as the compiler...
- spir (23/58) Nov 18 2010 =20
- Steven Schveighoffer (24/71) Nov 18 2010 const applied to a value type means the same thing as immutable. For
- Kagamin (2/15) Nov 18 2010 Doesn't immutability imply static storage? I also thought, it's a way to...
- Jonathan M Davis (10/29) Nov 18 2010 No. If it did, you couldn't initialize immutable stuff at runtime. Appar...
- Kagamin (2/10) Nov 20 2010 I see no difference. Both globals and locals can be initialized at runti...
- Jonathan M Davis (22/35) Nov 20 2010 I'm talking about direct initialization. If you do
In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable. So, the question is: what are the advantages of one over the other? Specifically, my concern is how likely compiler optimizations are. Does using immutable make compiler optimizations more likely? Or would const do just as well if not better? Or is dmd smart enough that it really doesn't matter if you use const or immutable on local variables which never change? - Jonathan M Davis
Nov 17 2010
Jonathan M Davis:In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable.In D2 for local variables that don't change use immutable when they are computed at run-time. I'd like to suggest you to use enum when they are known at compile-time, but in some cases this is bad (some examples of associative arrays, etc).So, the question is: what are the advantages of one over the other? Specifically, my concern is how likely compiler optimizations are. Does using immutable make compiler optimizations more likely? Or would const do just as well if not better? Or is dmd smart enough that it really doesn't matter if you use const or immutable on local variables which never change?Or is dmd dumb enough that it makes no optimization difference? :-) Bye, bearophile
Nov 17 2010
On Wednesday 17 November 2010 23:09:40 bearophile wrote:Jonathan M Davis:Well. yes. enums are definitely tha case for compile time constants. The question is for runtime. And why would you suggest immutable over const for runtime?In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable.In D2 for local variables that don't change use immutable when they are computed at run-time. I'd like to suggest you to use enum when they are known at compile-time, but in some cases this is bad (some examples of associative arrays, etc).I really don't see any reason why const vs immutable would make any difference for a local variable except insofar as a function takes an immutable argument rather than a const one. I would think that both would be optimized identically, but I don't know. - Jonathan M DavisSo, the question is: what are the advantages of one over the other? Specifically, my concern is how likely compiler optimizations are. Does using immutable make compiler optimizations more likely? Or would const do just as well if not better? Or is dmd smart enough that it really doesn't matter if you use const or immutable on local variables which never change?Or is dmd dumb enough that it makes no optimization difference? :-)
Nov 17 2010
On Wed, 2010-11-17 at 23:21 -0800, Jonathan M Davis wrote: [ . . . ]Well. yes. enums are definitely tha case for compile time constants. The =question=20is for runtime. And why would you suggest immutable over const for runtim=e? Why use enums rather than immutable for values that are known at compile time? immutable is really immutable whereas const implies that there is the possibility of change -- at least that is how I read the documentation and TDPL. [ . . . ]I really don't see any reason why const vs immutable would make any diffe=rence=20for a local variable except insofar as a function takes an immutable argu=ment=20rather than a const one. I would think that both would be optimized ident=ically,=20but I don't know.I am a fan of single assignment so I put immutable on all my variables except for loop control variables and accumulators. I haven't yet seen a need for const. Interesting, and possibly not irrelevant, side note: In a moment of complete stupidity I spelled immutable as invariant so had code like: invariant n =3D 1000000000 ; invariant delta =3D 1.0 / n ; instead of: immutable n =3D 1000000000 ; immutable delta =3D 1.0 / n ; and it all worked just fine. I have no idea how or why, but it did! --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel russel.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Nov 18 2010
On 18/11/2010 08:50, Russel Winder wrote:On Wed, 2010-11-17 at 23:21 -0800, Jonathan M Davis wrote: [ . . . ]invariant is the old deprecated name for immutable. It'll go away eventually. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.ukWell. yes. enums are definitely tha case for compile time constants. The question is for runtime. And why would you suggest immutable over const for runtime?Why use enums rather than immutable for values that are known at compile time? immutable is really immutable whereas const implies that there is the possibility of change -- at least that is how I read the documentation and TDPL. [ . . . ]I really don't see any reason why const vs immutable would make any difference for a local variable except insofar as a function takes an immutable argument rather than a const one. I would think that both would be optimized identically, but I don't know.I am a fan of single assignment so I put immutable on all my variables except for loop control variables and accumulators. I haven't yet seen a need for const. Interesting, and possibly not irrelevant, side note: In a moment of complete stupidity I spelled immutable as invariant so had code like: invariant n = 1000000000 ; invariant delta = 1.0 / n ; instead of: immutable n = 1000000000 ; immutable delta = 1.0 / n ; and it all worked just fine. I have no idea how or why, but it did!
Nov 18 2010
On Thu, 18 Nov 2010 08:50:58 +0000 Russel Winder <russel russel.org.uk> wrote:I am a fan of single assignment so I put immutable on all my variables except for loop control variables and accumulators. I haven't yet seen a need for const.For me, this is the default as well, meaning locals are constant. I would l= ove it to be the default for the language (or an option to set it so), so t= hat one would declare local _variables_. Which is never needed, and even sh= ows bad practice, except for symbols assignmed in loops, as you say. This extends to _value_ parameters, which are just locals as well (in other= words, "in" is the default for non-referenced parameters). Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 18 2010
On Thursday 18 November 2010 00:50:58 Russel Winder wrote:On Wed, 2010-11-17 at 23:21 -0800, Jonathan M Davis wrote: [ . . . ]enums _must_ known at compile time. immutable variables don't, so they won't actually be compile time constants. If you use immutable, it'll be created at runtime and be less efficient.Well. yes. enums are definitely tha case for compile time constants. The question is for runtime. And why would you suggest immutable over const for runtime?Why use enums rather than immutable for values that are known at compile time?immutable is really immutable whereas const implies that there is the possibility of change -- at least that is how I read the documentation and TDPL.Sure. You can change const if there are other references to the data which are mutable, but if there aren't then it makes no difference. And when dealing with value types, there really shouldn't be much - if any - difference between const and immutable, because you can't change them in either case. The real question here is whether there's any real difference in how well unnecessary local variables get optimized out when they're const or immutable. Do they disappear more as const? or as immutable? Or is it the same for both?It used to be that immutable wasn't a keyword. invariant was used. Some folks complained and Walter added immutable. So, immutable is supposed to be used for variables whereas invariant is just for invariants, but it still works to use invariant instead of immutable. - Jonathan M DavisI really don't see any reason why const vs immutable would make any difference for a local variable except insofar as a function takes an immutable argument rather than a const one. I would think that both would be optimized identically, but I don't know.I am a fan of single assignment so I put immutable on all my variables except for loop control variables and accumulators. I haven't yet seen a need for const. Interesting, and possibly not irrelevant, side note: In a moment of complete stupidity I spelled immutable as invariant so had code like: invariant n = 1000000000 ; invariant delta = 1.0 / n ; instead of: immutable n = 1000000000 ; immutable delta = 1.0 / n ; and it all worked just fine. I have no idea how or why, but it did!
Nov 18 2010
On Thu, 2010-11-18 at 01:27 -0800, Jonathan M Davis wrote: [ . . . ]enums _must_ known at compile time. immutable variables don't, so they wo=n't=20actually be compile time constants. If you use immutable, it'll be create=d at=20runtime and be less efficient.I guess I am tainted with the C/C++ prejudice that using enums for compile time constants was a hack because they didn't have const. When const arrived the compiler did all the right things and using enums became bad practice. The implication of the above is that DMD does not transform immutable values to instruction literals when it can, so using what is now deemed bad practice in C++ is correct idiomatic D? [ . . . ]The real question here is whether there's any real difference in how well==20unnecessary local variables get optimized out when they're const or immut=able.=20Do they disappear more as const? or as immutable? Or is it the same for b=oth? I have no idea ;-) I guess Walter need to comment on this. [ . . . ]=20 invariant n =3D 1000000000 ; invariant delta =3D 1.0 / n ; =20 instead of: =20 immutable n =3D 1000000000 ; immutable delta =3D 1.0 / n ;It used to be that immutable wasn't a keyword. invariant was used. Some f=olks=20complained and Walter added immutable. So, immutable is supposed to be us=ed for=20variables whereas invariant is just for invariants, but it still works to=use=20invariant instead of immutable.Aha, it wasn't a mistyping, it was a "before a change". Thanks for pointing the above out, it explains a lot. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel russel.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Nov 18 2010
On Wed, 17 Nov 2010 23:41:33 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote:In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable.immutable and const storage classes are identical as far as the compiler is concerned (neither can ever be changed). However, immutable gives more guarantees as a type modifier. I'd recommend immutable, because it's a specialization, and while the compiler can know that in a current function a const value is really immutable, it can't pass that knowledge to other functions. For example, a function may be overloaded on both const and immutable because the immutable one can make more assumptions. If you declare your variable const and call the function with your variable as the parameter, then it calls the const version, even though the data is really immutable. You lose out on some possible optimizations. Also, pure functions that take immutable can be 'strongly pure' so can be better optimized. All this is moot of course if your variable is a value type :) But I'd still recommend immutable even in those cases because the definition is clearer -- immutable data can never change, it is assumed that const data can change, but in your case, it will never change. If nothing else, it conveys the most accurate information to the reader of the code. -Steve
Nov 18 2010
On Thu, 18 Nov 2010 07:50:51 -0500 "Steven Schveighoffer" <schveiguy yahoo.com> wrote:On Wed, 17 Nov 2010 23:41:33 -0500, Jonathan M Davis <jmdavisProg gmx.com= =20 wrote: =20=20In C++, I tend to declare all local variables const when I know that =20 they aren't going to need to be altered. I'd like to something similar in D. =20 However, D has both const and immutable. I can see clear differences in how const and ==20immutable work with regards to function parameters and member variables, but it's==20not as clear with regards to const and immutable.=20 immutable and const storage classes are identical as far as the compiler =is concerned (neither can ever be changed). However, immutable gives mor=e =20guarantees as a type modifier. I'd recommend immutable, because it's a ==20specialization, and while the compiler can know that in a current functio=n =20a const value is really immutable, it can't pass that knowledge to other ==20functions. =20 For example, a function may be overloaded on both const and immutable =20 because the immutable one can make more assumptions. If you declare your==20variable const and call the function with your variable as the parameter,==20then it calls the const version, even though the data is really =20 immutable. You lose out on some possible optimizations. =20 Also, pure functions that take immutable can be 'strongly pure' so can be==20better optimized. =20 All this is moot of course if your variable is a value type :) But I'd ==20still recommend immutable even in those cases because the definition is ==20clearer -- immutable data can never change, it is assumed that const data==20can change, but in your case, it will never change. If nothing else, it ==20conveys the most accurate information to the reader of the code.Does all the thread finally mean that const is relative to variables, while= immutable is (as in FP) relative to elements (be them values or referenced= thingies)? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 18 2010
On Thu, 18 Nov 2010 14:30:34 -0500, spir <denis.spir gmail.com> wrote:On Thu, 18 Nov 2010 07:50:51 -0500 "Steven Schveighoffer" <schveiguy yahoo.com> wrote:const applied to a value type means the same thing as immutable. For example: const int i; immutable int i; both say the same thing -- i will never change. const applied to a *reference* type means that you cannot change that data *through that reference*, but it may change through another reference. For example: int i; const int *j = &i; I can change i, but not through j. immutable applied to a reference type means that *nobody* can change that data through any reference. It is safe to assume it never changes. For example: int i; immutable int j; immutable int *ip = &i; // Error! immutable int *jp = &j; // OK So while immutable and const are basically equivalent on value types (as was the original question), it's more 'correct' IMO to declare them as immutable because of the meaning of immutable -- this will never change, no matter what. -SteveOn Wed, 17 Nov 2010 23:41:33 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote:Does all the thread finally mean that const is relative to variables, while immutable is (as in FP) relative to elements (be them values or referenced thingies)?In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, butit'snot as clear with regards to const and immutable.immutable and const storage classes are identical as far as the compiler is concerned (neither can ever be changed). However, immutable gives more guarantees as a type modifier. I'd recommend immutable, because it's a specialization, and while the compiler can know that in a current function a const value is really immutable, it can't pass that knowledge to other functions. For example, a function may be overloaded on both const and immutable because the immutable one can make more assumptions. If you declare your variable const and call the function with your variable as the parameter, then it calls the const version, even though the data is really immutable. You lose out on some possible optimizations. Also, pure functions that take immutable can be 'strongly pure' so can be better optimized. All this is moot of course if your variable is a value type :) But I'd still recommend immutable even in those cases because the definition is clearer -- immutable data can never change, it is assumed that const data can change, but in your case, it will never change. If nothing else, it conveys the most accurate information to the reader of the code.
Nov 18 2010
Jonathan M Davis Wrote:In C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable. So, the question is: what are the advantages of one over the other? Specifically, my concern is how likely compiler optimizations are. Does using immutable make compiler optimizations more likely? Or would const do just as well if not better? Or is dmd smart enough that it really doesn't matter if you use const or immutable on local variables which never change? - Jonathan M DavisDoesn't immutability imply static storage? I also thought, it's a way to force CTFE.
Nov 18 2010
On Thursday, November 18, 2010 11:37:51 Kagamin wrote:Jonathan M Davis Wrote:No. If it did, you couldn't initialize immutable stuff at runtime. Apparently, in the case of globals or member variables (which have to be initialized statically anyway), it does mean that they could be optimized out (e.g. there's an open bug report on the fact that immutable fields in structs don't take any space), but that's no the case for local variables which can be initialized at runtime. An enum, on the other hand, _must_ be known at compile-time, and it's not going to take any storage (except for what it puts on the heap if it's a reference type), so enums are the way to declare compile-time constants in D. - Jonathan M DavisIn C++, I tend to declare all local variables const when I know that they aren't going to need to be altered. I'd like to something similar in D. However, D has both const and immutable. I can see clear differences in how const and immutable work with regards to function parameters and member variables, but it's not as clear with regards to const and immutable. So, the question is: what are the advantages of one over the other? Specifically, my concern is how likely compiler optimizations are. Does using immutable make compiler optimizations more likely? Or would const do just as well if not better? Or is dmd smart enough that it really doesn't matter if you use const or immutable on local variables which never change? - Jonathan M DavisDoesn't immutability imply static storage? I also thought, it's a way to force CTFE.
Nov 18 2010
Jonathan M Davis Wrote:I see no difference. Both globals and locals can be initialized at runtime and both can be static.Doesn't immutability imply static storage? I also thought, it's a way to force CTFE.No. If it did, you couldn't initialize immutable stuff at runtime. Apparently, in the case of globals or member variables (which have to be initialized statically anyway), it does mean that they could be optimized out (e.g. there's an open bug report on the fact that immutable fields in structs don't take any space), but that's no the case for local variables which can be initialized at runtime.
Nov 20 2010
On Saturday 20 November 2010 10:47:27 Kagamin wrote:Jonathan M Davis Wrote:I'm talking about direct initialization. If you do immutable a = func(); in global scope, func() _must_ be callable at compile-time. If you do that at local scope, it will be done at runtime. If you declare a local variable as static, then it's in the same boat as a global variable because it _is_ a global variable as far as its lifetime goes, just not its scope. However, notice that both auto a = func(); const a = func(); would have to have func() be callable at compile time if a is a global variable, whereas for a non-static local, it would be done at runtime. In comparison, enum a = func(); _always_ must have func() be callable at compile-time. immutable says nothing about whether a variable's value must be known at compile time. And CTFE only kicks in when the compiler _must_ do the initialization at compile time. Also, immutable by itself says nothing about static storage. It either has to be a global variable or be marked with static. And I believe that marking a global variable as static is redundant just like marking a method as public when it is already in a public block is redundant. static in C meant something for global variables, but C doesn't use modules. - Jonathan M DavisI see no difference. Both globals and locals can be initialized at runtime and both can be static.Doesn't immutability imply static storage? I also thought, it's a way to force CTFE.No. If it did, you couldn't initialize immutable stuff at runtime. Apparently, in the case of globals or member variables (which have to be initialized statically anyway), it does mean that they could be optimized out (e.g. there's an open bug report on the fact that immutable fields in structs don't take any space), but that's no the case for local variables which can be initialized at runtime.
Nov 20 2010