DIP69 - Implement scope for escape proof references

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 8 07:53:29 PST 2014


On 12/7/14 4:29 PM, Walter Bright wrote:
> On 12/7/2014 6:12 AM, Dicebot wrote:
>> But from existing cases it doesn't seem working good enough. For
>> example, not
>> being able to represent idiom of `scope ref int foo(scope ref int x) {
>> return x;
>> }` seems very limiting.
>
>    scope ref int foo(ref int x);
>
> will do it.

So:

int x;

foo(x) += 1;

will compile? I was under the impression this would be disallowed.

If you do not connect the scope to the parameter, then the caller has to 
assume that variable can escape, and you can't use scoped variables as 
arguments at all.

> My experience with C++ ref as type qualifier is very, very bad. It's a
> special case EVERYWHERE. Doing type deduction with it is an exercise in
> a completely baffling set of rules and a different rule for every
> occasion - Scott Meyers has a great piece on this.
>
> There are probably only a handful of people on the planet who actually
> understand C++ ref. I wished very hard to avoid that with D ref.
>

D has so many features that did not exist when ref was created (as inout 
in D1), that you can ALMOST duplicate ref. The only thing we could not 
duplicate is the implicit address-taking on construction, which maybe is 
not such a bad thing.

-Steve


More information about the Digitalmars-d mailing list