First Draft: ref For Variable Declarations

Walter Bright newshound2 at digitalmars.com
Sat Apr 13 16:12:29 UTC 2024


On 4/13/2024 3:24 AM, Dukc wrote:
> On Friday, 12 April 2024 at 20:43:50 UTC, Walter Bright wrote:
>> https://github.com/WalterBright/documents/blob/984374ca885e1cb10c2667cf872aebc13b4c1663/varRef.md
> 
> This hits the same problem my old 
> [DIP1022](https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1022.md) was 
> addressing. What should happen if `ref` variable is initialised with a RValue?

An error. The DIP says no rvalue references.


> In my opinion it should be an error, but:
>   - for `foreach` variables it is not

I think this is incorrect. Perhaps you meant static foreach?

>   - What if it's inside a template and is meant to be `ref` if possible but not 
> otherwise?
> The answer is the same as my DIP proposed: allowing `auto ref` for the variables.

Auto ref would be a separate thing. Let's keep this focused on this DIP.


> Is `ref` allowed for static / global / `shared` storage class values?

At the moment, no. I'm not sure there's a compelling purpose for it. Many times 
I've encountered a good use for local ref variables, but never for statics.


> All in all this is something that would sometimes be nice, and doesn't 
> immediately make me come up with any reason it wouldn't work. However it allows 
> to declare two variables that share memory, which gives me a feeling there might 
> be some loophole that I missed.

That's what @live addresses.


> Hopefully Timon will have a look. If my worry 
> turns out to be unfounded, I tend to be in favour of this.

Thank you.



More information about the dip.development mailing list