digitalmars.D.internals - Const or invariant key in dynamic array
- Valentino Giudice (28/30) Aug 23 Hi.
- Richard (Rikki) Andrew Cattermole (7/38) Aug 23 Associative array, not a dynamic array.
- Valentino Giudice (12/16) Aug 23 You are right, of course.
- SimonN (15/19) Sep 01 This deprecation warning during @safe was a bug in DMD 2.111 and
Hi. Consider the following snippet: ```D int[int] darr; darr[0] = 0; int k1 = 1; darr[k1] = 1; immutable k2 = 2; darr[k2] = 2; ``` The code declares a sparse array as a dynamic array with int as a type for both keys and values. If I place the code in a ` safe` context, the compiler warns me:Deprecation: using the result of a cast from `immutable(int)` to `int` as an lvalue will become ` system` in a future releaseBecause key values only need to be read, not written to, I'd argue that this code should be fine in a safe context. If the type of key is a composite one and it happens to contain pointers, references or dynamic arrays, then this prohibition actually makes sense. A user could iterate over keys, which would be returned as modifiable objects, then modify the referenced objects from there even if the key was originally created from a constant object. However, when the key type is a simple type or a struct type which contains no references, pointers or dynamic arrays, I'd argue this code should be allowed in a safe context. In fact, `T1[T2]`, `T1[const T2]` and `T1[immutable T2]` should be regarded as the same type. Granted, what I referenced is only a warning for now, but I hope it will be possible to use key values this way.
Aug 23
On Monday, 24 August 2026 at 02:04:10 UTC, Valentino Giudice wrote:Hi. Consider the following snippet: ```D int[int] darr; darr[0] = 0; int k1 = 1; darr[k1] = 1; immutable k2 = 2; darr[k2] = 2; ``` The code declares a sparse array as a dynamic array with int as a type for both keys and values.Associative array, not a dynamic array.If I place the code in a ` safe` context, the compiler warns me:So basically to clarify: As long as a struct is by-value, is POD, allow mutable, otherwise require immutable. Note that const isn't suitable for keys of a map.Deprecation: using the result of a cast from `immutable(int)` to `int` as an lvalue will become ` system` in a future releaseBecause key values only need to be read, not written to, I'd argue that this code should be fine in a safe context. If the type of key is a composite one and it happens to contain pointers, references or dynamic arrays, then this prohibition actually makes sense. A user could iterate over keys, which would be returned as modifiable objects, then modify the referenced objects from there even if the key was originally created from a constant object. However, when the key type is a simple type or a struct type which contains no references, pointers or dynamic arrays, I'd argue this code should be allowed in a safe context. In fact, `T1[T2]`, `T1[const T2]` and `T1[immutable T2]` should be regarded as the same type. Granted, what I referenced is only a warning for now, but I hope it will be possible to use key values this way.
Aug 23
On Monday, 24 August 2026 at 02:11:24 UTC, Richard (Rikki) Andrew Cattermole wrote:Associative array, not a dynamic array.You are right, of course. I know the terminology, yet I repeated the mistake several times, including in the title. My apologies.So basically to clarify: As long as a struct is by-value, is POD, allow mutable, otherwise require immutable.Currently to avoid the wording it is required that the index is mutable, if it is in the type. What I am suggesting is that if the key is a simple value or a POD, then I should be able to use a constant or immutable key value without getting a warning, as I don't see how it can lead to any issue.
Aug 23
On Monday, 24 August 2026 at 02:55:16 UTC, Valentino Giudice wrote:What I am suggesting is that if the key is a simple value or a POD, then I should be able to use a constant or immutable key value without getting a warning, as I don't see how it can lead to any issue.This deprecation warning during safe was a bug in DMD 2.111 and DMD 2.112. A fix has been available since December 2025: https://github.com/dlang/dmd/pull/22208 The fix will make it into DMD 2.113.0. Now, the compiler will not print a deprecation warning anymore for this compiler-generated cast. It's perfectly fine for usercode to index like that in safe code. Keep your code. :-) With DMD 2.112 or older, you can work around this message by hiding all deprecation warnings. Pass `-d` to DMD, or set the `DFLAGS` environment variable to `-d` (on Linux: `export DFLAGS=-d`) and then build with DMD or dub. -- Simon
Sep 01








SimonN <eiderdaus gmail.com>