+1 vote
in D Programming by
What Is Immutable Good For?

1 Answer

0 votes
by

Immutable data, once initialized, is never changed. This has many uses:

  • Access to immutable data need not be synchronized when multiple threads read it.
  • Data races, tearing, sequential consistency, and cache consistency are all non-issues when working with immutable data.
  • Pure functions can only accept immutable parameters.
  • When doing a deep copy of a data structure, the immutable portions need not be copied.
  • Invariance allows a large chunk of data to be treated as a value type even if it is passed around by reference (strings are the most common case of this).
  • Immutable type provides more self-documenting information to the programmer.
  • Immutable data can be placed in hardware protected read-only memory, or even in ROMs.
  • If immutable data does change, it is a sure sign of a memory corruption bug, and it is possible to automatically check for such data integrity.
  • Immutable types provide for many program optimization opportunities.
  • const acts as a bridge between the mutable and immutable worlds, so a single function can be used to accept both types of arguments.

Related questions

+1 vote
asked Mar 5, 2021 in D Programming by SakshiSharma
0 votes
asked Mar 5, 2021 in D Programming by SakshiSharma
...