First Draft: ref For Variable Declarations

Timon Gehr timon.gehr at gmx.ch
Sun May 5 09:39:17 UTC 2024


On 5/2/24 08:51, Walter Bright wrote:
>> (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.

Yes, exactly. My point was we need to think about that anyway. There are 
other things that ought not be rebindable:

```d
@safe:
class S{
     immutable int x;
     this(int y){
         foo();
         x=y;
         foo();
     }
     static int[immutable(int)*] t;
     void foo(){
         if(&x in t) assert(t[&x]==x); // error, immutable data modified
         t[&x]=x;
     }
}

void main(){
     auto s=new S(2);
}
```

Of course, with `ref` there is an additional challenge: it cannot be 
default-initialized.


More information about the dip.development mailing list