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

Walter Bright via Dlang-internal dlang-internal at puremagic.com
Sat Oct 22 00:38:18 PDT 2016


On 10/20/2016 10:46 PM, Dicebot wrote:
> Wait what?

I've said it multiple times. 'ref' cannot be converted to '*' and be safe. Hence 
the compiler disallows it.

This is exactly why we need to start with trivial examples.


> This is in direct contradiction with what DIP1000 states and
> what I have been trying to write example of all this time.

I know what you've been trying to write, and each time I point out the same thing.


> By existing
> specification annotating method with `return scope` means that lifetime
> of return value is same as that method `this`.

If this is a class this then return scope is appropriate, because this is a 
pointer. But when this is a ref, scope then applies to the struct's fields.


> Are you saying that existing system forgets that `instance` is `this`
> the very moment `borrow` methods finishes? If yes, I am afraid it is a
> rather useless feature, much worse than one may think reading DIP1000.

By making borrow @trusted in order to defeat the checking, you defeated the 
connection between 'instance' the argument and the return value from the function.

Do not convert 'ref' to '*'. It won't work.


>> The practical result is that a container that is passed by ref can only
>> control its uses by returning by ref, not by *.
>
> 1) Why? You still haven't answered how scope pointer is different from
> ref in @safe code.

I've been trying to answer. There are two things at work for variable 'v':

1. v's address
2. v's value

'ref' deals with the first, 'scope' with the second. It is very, very, very 
important to think about whether you are dealing with v's address or v's value, 
otherwise you will get hopelessly lost.

Next, recall that 'this' for a struct is an address. 'this' for a class is a value.

> 2) Does that imply that lifetime algebra as described in DIP1000 only
> applies to function internal arguments and variables? As it is not
> possible to neither have `ref` variables, nor take address of `ref`
> return value, lifetime of return value will always be limited to single
> expression.

That's what scope variables are for - storing addresses in.

I implore you to look at the test cases with the PR. They are all trivial and 
easy to follow.



More information about the Dlang-internal mailing list