[Dlang-internal] DIP1000 discussion and testing: borrowing a range

Dicebot via Dlang-internal dlang-internal at puremagic.com
Thu Oct 20 09:37:23 PDT 2016


On 10/16/2016 10:51 PM, Walter Bright wrote:
> As for error [2], 'ref' is meant to be a restricted pointer. Converting
> a restricted pointer to a regular pointer, which is what &this does,
> undermines the whole point of having 'ref', and so is disallowed in
> @safe code.
> 
> Consider:
> 
>   @safe int* foo(return ref int r) {
>     return &r; // Error: cannot take address of parameter r in @safe
> function foo
>   }

This must not compile indeed. But this is perfectly fine:

@safe scope int* foo (return scope ref int r) {
    return &r; // taking address of r is fine because returned pointer
can't outlive the reference
}

As far as I can see right now, from the safety PoV scoped pointer is
strictly equivalent to ref.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/dlang-internal/attachments/20161020/25e79445/attachment.sig>


More information about the Dlang-internal mailing list