www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Declaring variables const vs immutable

reply Brother Bill <brotherbill mail.com> writes:
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?
Nov 09
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Sunday, 9 November 2025 at 23:17:44 UTC, Brother Bill wrote:

 My question is how does D treat these types differently?
 When should one pick one vs. the other?
My answer from before: https://forum.dlang.org/post/wkrdjxxkjzrtwqzmwijx forum.dlang.org -Steve
Nov 09
parent Brother Bill <brotherbill mail.com> writes:
On Monday, 10 November 2025 at 04:47:44 UTC, Steven Schveighoffer 
wrote:
 On Sunday, 9 November 2025 at 23:17:44 UTC, Brother Bill wrote:

 My question is how does D treat these types differently?
 When should one pick one vs. the other?
My answer from before: https://forum.dlang.org/post/wkrdjxxkjzrtwqzmwijx forum.dlang.org -Steve
Thank you. I missed that you had already covered this question.
Nov 09