Run-time setting of immutable variable?

Ali Çehreli acehreli at yahoo.com
Thu Sep 2 23:29:46 UTC 2021


On 9/2/21 9:01 AM, DLearner wrote:
> Suppose there is a variable that is set once per run, and is (supposed) 
> never to be altered again.

An accessor function can be a solution, which supports your other 
comment about data potentially mutating by other means:

// Assume these are in a module
// vvvvvvvvvvvvvvvvvvvvvvvvvvvv

// This is private; you can even name it arr_.
private
ubyte[10] arr;

// This is the accessor:
public
const(ubyte)* arrPtr() {
   return arr.ptr;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

void main() {
   *arrPtr = 42;  // Compilation ERROR; good.
}

Ali


More information about the Digitalmars-d-learn mailing list