First Draft: ref For Variable Declarations

Dukc ajieskola at gmail.com
Sat Apr 13 18:22:58 UTC 2024


On Saturday, 13 April 2024 at 16:12:29 UTC, Walter Bright wrote:
> On 4/13/2024 3:24 AM, Dukc wrote:
>> What should happen if `ref` variable is initialised with a 
>> RValue?
>
> An error. The DIP says no rvalue references.
>

Good so far...

>
>> 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?

No I didn't. This compiles and does nothing:

```D
@safe void main()
{
     foreach(ref el; 0 .. 5) el = 42;
}
```

...which means that if `ref int = 42;` fails to compile, as it 
should, it's inconsistent with the present behaviour!


>> 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.
>

No problem - but the dip should explicitly say it's for local 
variables then, not for variables in general.

>
>> 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.

To be clear I'm happy with variables sharing memory, if it 
doesn't break type safety in any way. I just have a suspicion 
that if it does so, it might be hard to notice at least for me.




More information about the dip.development mailing list