Run-time setting of immutable variable?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Sep 2 16:19:47 UTC 2021


On Thu, Sep 02, 2021 at 04:01:19PM +0000, DLearner via Digitalmars-d-learn wrote:
> Suppose there is a variable that is set once per run, and is
> (supposed) never to be altered again.  However, the value to which it
> is set is not known at compile time.

This is the classic use case of `immutable`.  Using the example you
gave, you'd move the initialization of ArrPtr into a static module
constructor:

	immutable void* ArrPtr;
	shared static this() {
		ArrPtr = ...; // initialize it here
	}

	void main() {
		... // ArrPtr is immutable from here on.
	}


T

-- 
Дерево держится корнями, а человек - друзьями.


More information about the Digitalmars-d-learn mailing list