ref is unsafe

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 4 12:19:48 PST 2013


On Friday, January 04, 2013 17:26:59 Zach the Mystic wrote:
> > Honestly though, I'm inclined to argue that functions which
> > return by ref and
> > have a ref parameter of that same type just be considered
> > @system.
> 
> Structs mess that up as well:
> struct S { int i; }
> ref int d(ref S s)
> {
> return s.i;
> }

Yes. That's a function which takes a ref and returns by ref just like I said. 
It's just that in this case, the ref returned isn't the full object that was 
passed by ref but just a portion of it. What that means is that you can't 
assume that the ref being returned is safe just because the type of the 
parameter and the return type aren't the same. But it doesn't change the 
statement that a function which takes a parameter by ref and returns by ref 
can't be considered @safe without additional constraints of some kind. It just 
shows why you don't have an easy way out to make many of them @safe based on 
the differing types involved.

- Jonathan M Davis


More information about the Digitalmars-d mailing list