Tuesday, 20 May 2014

D - Immutable

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

D - 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.
  • There can be math constants like pi that will never change.
  • There can be arrays where we want to retain values and it is not requirements of mutation.
Immutability makes it possible for 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

There are three types of defining variables that can never be mutated.
  • enum constants
  • immutable variables
  • const variables

enum constants

enum constants makes it possible for us 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");
   }
}
If we compile and run above code, this would produce the following result:
The day is Sunday

immutable variables

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);
}
If we compile and run above code, this would produce 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

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

Immutable parameters

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);
}
If we compile and run above code, this would produce the following result:
0: 1
1: 2

0 comments: