DIP1000: Scoped Pointers (Discussion)

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 14 07:37:41 PDT 2016


On 08/10/2016 11:36 PM, Dicebot wrote:
> http://forum.dlang.org/post/pqsiqmkxenrwxoruzaml@forum.dlang.org

I am still uncertain myself about the proposal, mostly because can't
completely grasp rules for returning as scope. My litmus test example is
very simple (for desired semantics):

```
struct Container
{
    int data;

    static struct Range
    {
        int* pdata;
        // range methods skipped for clarity ..
    }

    Range asRange ( )
    {
        return Range(&this.data);
    }
}

void main ( )
{
    Container container;

    import std.stdio;
    writeln(container.asRange()); // case 1, OK

    scope r = container.asRange(); // case 2, OK
    auto r = container.asRange(); // case 3, not OK
}
```

It looks like
(https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md#scope-function-returns)
I can annotate `scope Range asRange ( )` to force result into rvalue and
make case 1 and case 3 work as desired but there is no way to express
lifetime relation between returned struct and host container so that
case 2 will work.

If it is indeed correct, I am going to call it a complete showstopper.
Any scope proposal is of interest to me only if it can be used to
implement some form of borrowship semantics (even awkwardly looking) on
top - everything else is a nice addition to have but in no way justifies
added language weight.

Note that section
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md#owning-containers
explain similar example (but with refcounting instead of stack
allocation) but it does not show semantics at call site and is either
subject to same limitation (can't store result of `opIndex`) or
contradicts previous spec.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160814/171882e5/attachment.sig>


More information about the Digitalmars-d mailing list