www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Should I prefer immutable or const?

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Should I always, when possible, prefer `immutable` over `const`?

And does `immutable` increase the possibility of the compiler 
doing optimizations, such as common subexpression elimination?

Or can the compiler infer `const` declarations to also be 
`immutable`?
Nov 01 2016
next sibling parent ag0aep6g <anonymous example.com> writes:
On 11/01/2016 06:52 PM, Nordl├Âw wrote:
 Should I always, when possible, prefer `immutable` over `const`?
I'd say: prefer immutable.
 And does `immutable` increase the possibility of the compiler doing
 optimizations, such as common subexpression elimination?

 Or can the compiler infer `const` declarations to also be `immutable`?
A `const int x = 42;` is effectively immutable, but its type is const(int), not immutable(int). A compiler may be smart enough to treat it as immutable with regards to optimizations, but I wouldn't expect dmd to be that smart, or only in very simple cases. ldc and gdc are probably better at it. However, for function overloads and such the compiler must consider the type, not any additional information it has about the data. So if some function has an optimized overload for immutable, that can only be taken when you type out "immutable".
Nov 01 2016
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Tuesday, November 01, 2016 17:52:56 Nordl÷w via Digitalmars-d-learn 
wrote:
 Should I always, when possible, prefer `immutable` over `const`?

 And does `immutable` increase the possibility of the compiler
 doing optimizations, such as common subexpression elimination?

 Or can the compiler infer `const` declarations to also be
 `immutable`?
That's not really something that can have a generally applicable answer. const and immutable are similar but also very different, and which is appropriate depends entirely on what you're doing. If you're dealing with built-in value types, I would expect the code to be 100% identical, so whether you use const or immutable is a matter of preference. I tend to use immutable, since it clearly indicates that the value will never change, whereas for some types, const can change thanks to another reference to the same data, but it doesn't actually matter for a value type. For reference types, if you want to use immutable, you're constructing the object as immutable and using it as immutable everywhere. It's not a temporary thing. In contrast, const could refer to either an immutable or mutable object, so all you're really saying is that that code won't mutate the object, not that its value won't change. It really doesn't make sense to use const if you're intending that the object always be const. If you're doing that, you might as well make it immutable, since you get implicit sharing out of it, and the compiler can do optmizations based on immutable that it can't do based on const, because it knows that the immutable object will never change, whereas for a const reference to an object, it just knows that _that_ code can't change it through _that_ reference. The same function could very well be mutating that object via a mutable reference to the same data. Also, _very_ little in the way of optimizations can be done based on const, so if you're looking to get the compiler to optimize stuff better, then immutable is unquestionably better than const, but it does mean that that data can never be mutated. Really, const is for the cases where you want to accept mutable, const, and immutable data without necessarily templatizing your function, and you don't want the function to mutate the data. So, it's for passing off data to be looked at but not mutated and not really for storing it. You _can_ store it as const, but there's pretty much always going to be a mutable reference to that data somewhere else, otherwise there wasn't much point in using const over immutable. Also, keep in mind that const is annoyingly restrictive in D, because it's transitive and has no backdoors that aren't undefined behavior (unlike C++), so slapping const on a lot of stuff is going to tend to prevent you from doing useful things. In general, I'd expect stuff to be either mutable or immutable and then primarily use templates to share code with const only being used sparingly, but some folks do try and use const a fair bit - especially when first coming from C++. So, hopefully that's at least somewhat helpful, but to a great extent, whether const makes any sense is highly dependent on what your code is doing. And aside from specially designed types, immutable isn't likely to be used much outside of value types of arrays, because not being able to ever mutate under any circumstances doesn't usually work well with types unless they were designed with that in mind. - Jonathan M Davis
Nov 01 2016
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 1 November 2016 at 18:55:26 UTC, Jonathan M Davis 
wrote:
 - Jonathan M Davis
Thanks. I'll go for immutable, when possible, then. I wish I had a shorter way to write immutable, though :) Do you think it would be possible to adopt Rust's syntax `let`?
Nov 02 2016
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, November 02, 2016 23:23:22 Nordl÷w via Digitalmars-d-learn 
wrote:
 On Tuesday, 1 November 2016 at 18:55:26 UTC, Jonathan M Davis

 wrote:
 - Jonathan M Davis
Thanks. I'll go for immutable, when possible, then. I wish I had a shorter way to write immutable, though :) Do you think it would be possible to adopt Rust's syntax `let`?
I'm not very familiar with rust, so I don't know, but in general, at this point, you'd need a pretty solid argument why a syntactic change to the language was a big improvement, or it wouldn't happen. It also would likely require that someone put in the time and effort to produce a solid DIP in favor of it, and from how Andrei has been responding to DIP-related stuff of late, it sounds like he and Walter are setting the bar very high right now. And I'd guess that simply not liking how many characters it takes to type immutable is nowhere near a sufficient reason to get anything changed. But maybe a solid DIP would stand a chance. I don't know. - Jonathan M Davis
Nov 02 2016