ref is unsafe

Jonathan M Davis jmdavisProg at gmx.com
Sun Dec 30 14:29:33 PST 2012


On Sunday, December 30, 2012 23:18:43 jerro 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.
> 
> What about this:
> 
> struct Foo
> {
>      int a;
> }
> 
> ref int bar(ref Foo foo)
> {
>      return foo.a;
> }
> 
> the parameter type and the return type here are different, but
> bar still returns a reference to its parameter. I guess you
> should consider all functions that return ref and have at least
> one ref parameter @system (unless they are marked @trusted).

Good point. Member variables of parameters also cause problems. So, it very 
quickly devolves to any function which accepts a user-defined type by ref and 
returns anything by ref would have to be @system, which is far from pleasant.

- Jonathan M Davis


More information about the Digitalmars-d mailing list