Re: Run-time setting of immutable variable?

2021-09-03 Thread DLearner via Digitalmars-d-learn
On Thursday, 2 September 2021 at 23:12:28 UTC, Steven Schveighoffer wrote: [...] immutable means "I can never change and *everything I point at* can never change". [...] If that is how the language defines the keyword 'immutable' when used in the definition of a pointer variable, then so be

Re: Run-time setting of immutable variable?

2021-09-02 Thread Ali Çehreli via Digitalmars-d-learn
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 // v

Re: Run-time setting of immutable variable?

2021-09-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/2/21 1:17 PM, DLearner wrote: I am looking for a mutable Arr but would like an immutable ArrPtr. Then you want const not immutable. Here is the reason: ```d void main() { int x = 5; immutable int *ptr = cast(immutable int *)&x; assert(*ptr == 5); // ok x = 6; assert(

Re: Run-time setting of immutable variable?

2021-09-02 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 02, 2021 at 05:17:15PM +, DLearner via Digitalmars-d-learn wrote: [...] > The following clean-compiled and produced the expected result: > ``` > ubyte[10] Arr; > > immutable void* ArrPtr; > shared static this() { > ArrPtr = cast(immutable void*)(&Arr[0]

Re: Run-time setting of immutable variable?

2021-09-02 Thread jfondren via Digitalmars-d-learn
On Thursday, 2 September 2021 at 17:17:15 UTC, DLearner wrote: Surely there is no inconsistency - at run time the array is in a fixed place, so ArrPtr is (or at least should be) a constant, but the contents of the array can vary as the program runs. In the case of `immutable(T)* ArrPtr`, the

Re: Run-time setting of immutable variable?

2021-09-02 Thread Kagamin via Digitalmars-d-learn
If you want only address, you can keep it as size_t: ubyte[10] Arr; immutable size_t Address; static this() { Address = cast(size_t)(&Arr[0]); }

Re: Run-time setting of immutable variable?

2021-09-02 Thread DLearner via Digitalmars-d-learn
On Thursday, 2 September 2021 at 16:46:46 UTC, Steven Schveighoffer wrote: On 9/2/21 12:01 PM, DLearner 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. Example below,

Re: Run-time setting of immutable variable?

2021-09-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/2/21 12:01 PM, DLearner 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. Example below, variable is 'ArrPtr'; ``` ubyte[10] Arr; // immutable void* ArrPtr; void* A

Re: Run-time setting of immutable variable?

2021-09-02 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 02, 2021 at 04:01:19PM +, 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 `immutabl

Run-time setting of immutable variable?

2021-09-02 Thread DLearner via Digitalmars-d-learn
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. Example below, variable is 'ArrPtr'; ``` ubyte[10] Arr; // immutable void* ArrPtr; void* ArrPtr; void main() { ArrPtr = ca