This topic is about D Programming – Immutable.
We often use variables that are mutable but there can be many occasions mutability is not required. Immutable variables can be used in such cases. A few examples are given below where immutable variable can be used.
- In case of math constants such as pi that never change.
- In case of arrays where we want to retain values and it is not requirements of mutation.
Immutability makes it possible to understand whether the variables are immutable or mutable guaranteeing that certain operations do not change certain variables. It also reduces the risk of certain types of program errors. The immutability concept of D is represented by the const and immutable keywords. Although the two words themselves are close in meaning, their responsibilities in programs are different and they are sometimes incompatible.
The immutability concept of D is represented by the const and immutable keywords. Although the two words themselves are close in meaning, their responsibilities in programs are different and they are sometimes incompatible.
Types of Immutable Variables in D
There are three types of defining variables that can never be mutated.
- enum constants
- immutable variables
- const variables
enum Constants in D
The enum constants makes it possible to relate constant values to meaningful names. A simple example is shown below.
Example
import std.stdio; enum Day{ Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } void main() { Day day; day = Day.Sunday; if (day == Day.Sunday) { writeln("The day is Sunday"); } }
When the above code is compiled and executed, it produces the following result −
The day is Sunday
Immutable Variables in D
Immutable variables can be determined during the execution of the program. It just directs the compiler that after the initialisation, it becomes immutable. A simple example is shown below.
Example
import std.stdio; import std.random; void main() { int min = 1; int max = 10; immutable number = uniform(min, max + 1); // cannot modify immutable expression number // number = 34; typeof(number) value = 100; writeln(typeof(number).stringof, number); writeln(typeof(value).stringof, value); }
When the above code is compiled and executed, it produces the following result −
immutable(int)4 immutable(int)100
You can see in the above example how it is possible to transfer the data type to another variable and use stringof while printing.
Const Variables in D
Const variables cannot be modified similar to immutable. immutable variables can be passed to functions as their immutable parameters and hence it is recommended to use immutable over const. The same example used earlier is modified for const as shown below.
Example
import std.stdio; import std.random; void main() { int min = 1; int max = 10; const number = uniform(min, max + 1); // cannot modify const expression number| // number = 34; typeof(number) value = 100; writeln(typeof(number).stringof, number); writeln(typeof(value).stringof, value); }
If we compile and run above code, this would produce the following result −
const(int)7 const(int)100
Immutable Parameters in D
const erases the information about whether the original variable is mutable or immutable and hence using immutable makes it pass it other functions with the original type retained. A simple example is shown below.
Example
import std.stdio; void print(immutable int[] array) { foreach (i, element; array) { writefln("%s: %s", i, element); } } void main() { immutable int[] array = [ 1, 2 ]; print(array); }
When the above code is compiled and executed, it produces the following result −
0: 1 1: 2
In this topic we learned about D Programming – Immutable. To learn more, Click Here.
Pingback: D Programming - Templates - Adglob Infosystem Pvt Ltd