First Draft: ref For Variable Declarations

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Apr 17 14:07:04 UTC 2024


On 17/04/2024 9:20 PM, Nick Treleaven wrote:
> 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`.

Currently the DIP only shows aliasing of variables, expressions like the 
above do not appear to be supported.



More information about the dip.development mailing list