First Draft: ref For Variable Declarations

Walter Bright newshound2 at digitalmars.com
Thu May 2 06:51:12 UTC 2024


On 5/1/2024 2:10 PM, Timon Gehr wrote:
> I am not sure how, but I had missed this thread so far. I am very much in favor, 
> I have wished for this many times over the years. I think the only risk is 
> implementation bugs in safety checks. The DIP seems a bit incomplete in this 
> department, e.g., it does not say what happens when you take the address of a 
> `ref` local. Maybe it can point to some prior art, but just stating that it 
> cannot be returned seems neither sufficient nor necessary.

The same thing happens when you take the address of a ref parameter - you get 
the address of what the ref refers to.

I did my best to make it match exactly what the existing semantics of `ref` are. 
If I made a mistake, we can fix it when we find it.


> (Of course, it would be even better if fields could also be `ref`, but then you 
> get into initialization safety. This is a general soundness problem in the 
> current language though.)

`ref` is not rebindable, so a `ref` field means that support is needed in the 
constructor or default initializer, which needs some investigation on how to do 
it right.


More information about the dip.development mailing list