Declaring variables const vs immutable
Brother Bill
brotherbill at mail.com
Sun Nov 9 23:17:44 UTC 2025
When declaring a variable, we may add const or immutable to its
type.
```
const int a = 86; // typeof(a).stringof is const(int)
immutable int b = 99; // typeof(b).stringof is immutable(int)
const int[] array_a = [42, 100]; // typeof(array_a).stringof
is const(int[])
immutable int[] array_b = [42, 100]; // typeof(array_b).stringof
is immutable(int[])
```
They seem to have identical behavior, at least in the simple case.
My question is how does D treat these types differently?
When should one pick one vs. the other?
More information about the Digitalmars-d-learn
mailing list