First Draft: ref For Variable Declarations
Nick Treleaven
nick at geany.org
Wed Apr 17 09:20:55 UTC 2024
On Friday, 12 April 2024 at 20:43:50 UTC, Walter Bright wrote:
> https://github.com/WalterBright/documents/blob/984374ca885e1cb10c2667cf872aebc13b4c1663/varRef.md
Ref variables would be useful for refactoring. E.g. to disallow
foreach over an array with a non-const `ref` index - related to:
https://issues.dlang.org/show_bug.cgi?id=24499
```d
foreach (ref i, ref e; array) {
// code that modifies `i`, causing skipped/repeated elements
of foreach
```
Rewrite to:
```d
for (size_t i; i < array.length; i++) {
ref e = array[i];
// code that modifies `i`, which is now less surprising
```
Currently you'd have to write `ref e() => array[i];`, which is
less intuitive and is not the same if `array` is reassigned
between calls to `e`.
More information about the dip.development
mailing list