Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and how should I use `const` and `immutable` in D?

In many modern languages const correctness should be used to clarify interfaces and intent as well as to provide some opportunities to the compiler to optimize. In D there's the cool feature of really immutable data. How should const and immutable be used? I kinda figured, that preferring const as qualifiers for functions arguments and immutable for local variables seems to be a good way to write code, but as soon as you want to assign a struct with a reference or pointer like data member (private or not) to an immutable variable you can't do that.

struct S {
    private int[] data;
}

void main() {
    immutable s = new S; // won't work, since members aren't immutable
}

Hence, changing the implementation of a struct can break my code, if I use immutable. Should I prefer const for local variables and use immutable only when necessary? What are the guidelines?

like image 459
Ralph Tandetzky Avatar asked May 15 '13 08:05

Ralph Tandetzky


People also ask

What is the difference between immutable and constant?

Difference between constant and immutable variableA constant variable is assigned a value at the time of declaration. On the other hand, immutable is also used for the same purpose but is a little flexible as it can be assigned inside a constructor at the time of its construction.

What is the difference between constant and immutable state variables?

State variables can be declared as constant or immutable . In both cases, the variables cannot be modified after the contract has been constructed. For constant variables, the value has to be fixed at compile-time, while for immutable , it can still be assigned at construction time.

Does const make a variable immutable?

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

Is const immutable C++?

A C++ const-qualified stack/global object would be considered a shallow immutable object. That is, without escape hatches, developers cannot create non-const references (including through pointers) to such a const-qualified object.


1 Answers

You are extremely lucky - a video from DConf2013 with presentation devoted exactly to this topic has been published last week : http://youtu.be/mPr2UspS0fE

In your case, auto s = new immutable(S)(); should do the trick. You can then create data slice points to in constructor. However, if this slice may point to any data, then S can't possibly be immutable, because both const and immutable are transitive in D - they provide very strong guarantees not only about variable itself but also about any data that can be accessed from it indirectly via reference/pointer.

It is actually covered in linked video, but short summary is that you want to use immutable when really intend to. In other words, when you want your code to break if S implementation changes so that immutable guarantees are no longer valid.

like image 181
Mihails Strasuns Avatar answered Jan 03 '23 18:01

Mihails Strasuns