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 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.
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.
There are three types of defining variables that can never be mutated.
enum constants
immutable variables
const variables
The enum constants makes it possible to relate constant values to meaningful names. A simple example is shown below.
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 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.
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 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.
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

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.
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


    10 Lectures 
     1.5 hours 

 Nishant Malik

    42 Lectures 
     1.5 hours 

 Ravi Kiran

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    14 Lectures 
     1 hours 

 Mahesh Kumar

    18 Lectures 
     49 mins

 Rushi Panchal
 Print
 Add Notes
 Bookmark this page