First Draft: ref For Variable Declarations

Dukc ajieskola at gmail.com
Fri Apr 19 08:44:20 UTC 2024


On Wednesday, 17 April 2024 at 16:51:41 UTC, Dennis wrote:
> On Wednesday, 17 April 2024 at 14:29:17 UTC, Dukc wrote:
>> If that was the case this DIP would be completely useless, as 
>> you could already do everything it enabled with `alias`.
>
> I currently sometimes use pointers to alias members.
>
> ```D
> // Inside struct LightOptimizer
> void moveLightGlobal()
> {
>     auto light = &this.scene.light;
>     light.pos += vec3(...);
> }
>
> void onMouseclick(ref GameState gs)
> {
>     auto ui = &gs.ui;
>     ui.startDrag();
>     ...
> }
> ```
>
> I'd prefer using `ref` since that's safer and enables opIndex 
> overloads, so this DIP seems like a nice addition to me.

Yes I agree. I was just responding to the notion that the DIP 
would only allow referring to variables, not expressions. To me 
it doesn't seem to say that, and I'm sure the intended meaning is 
that expressions are fine as long as they are lvalues.

> I tried replacing those cases with alias but it errors:
> ```
> Error: accessing non-static variable `scene` requires an 
> instance of `LightOptimizer`
> ```

Well this can be arguably seen as a "variable" as opposed to 
"expression", in which case I was wrong - the DIP would be of 
some (but much lesser) use even if it really allowed only 
references to variables. But no one is advocating for that 
restriction so it doesn't matter. At maximum the DIP writing is 
ambiguous on this matter.


More information about the dip.development mailing list