On 13/09/2012 16:29, David Piepgrass wrote:
On Thursday, 13 September 2012 at 15:01:28 UTC, Andrei Alexandrescu wrote:
On 9/13/12 10:53 AM, David Piepgrass wrote:
Walter and I have discussed this for quite a while. We have recently
decided to disallow, at least in SafeD, escaping the address of a ref
parameter. In the beginning we'll be overly conservative by
disallowing taking the address of a ref altogether. I'll write a DIP
on that soon.

Err, wouldn't that break a lot of stuff, a lot of which is actually safe
code?

void a(ref int x) { b(&x); }
void b(int* x) { if(x != null) (*x)++; }

Yes. Disallowing taking the address of a local is conservative and
would disallow a number of valid programs.

What about:

void b(scope int* x) { if(x != null) (*x)++; }

Then 'a' could compile safely.

Arguably, such programs are in poor style anyway. A good program takes
pointers only if it needs to keep them around; if all that's needed is
to use the parameter transitorily or pass it down, ref is best.

Another common reason to use a pointer (instead of ref) is if it's
optional (nullable). If the parameter is ref then the caller must go to
the trouble of creating a variable.

Maybe std.typecons.Nullable or something like it helps there.

Reply via email to