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

Dicebot via Dlang-internal dlang-internal at puremagic.com
Sun Oct 16 07:02:55 PDT 2016


On 10/16/2016 04:45 PM, Dicebot wrote:
> I'll start separate sub-threads with more details.

This is probably most ambitious snippet I have come up with so far. It
seems to be possible within current DIP1000 definition but doesn't work
for other reasons I wanted to discuss:

------------------------------------------------------------
@safe unittest
{
    @safe static struct Owner
    {
        int data;

        // must bind lifetime of return pointer/ref to one of `this`
	// Error: function has 'return' but does not return any
 	// indirections
        auto borrow () return scope
        {
            struct Range
            {
                int* data;
                enum empty = false;
                auto front () { return *this.data; }
                void popFront() { (*this.data)++; }
            }

            return Range(&this.data); // Error: cannot take address of
                                      // parameter this in @safe
function borrow
        }
    }

    Owner instance;
    // must work, lifetime of 'instance' exceeds one of x :
    scope x = instance.borrow();
    // must not work, infinite lifetime
    auto y  = instance.borrow();
}
------------------------------------------------------------

First error simply to be simply a bug, because it contradicts "struct is
a composition of its fields" statement from DIP1000.

Second error is an existing limitation of @safe code (can't take pointer
from ref) which seems extremely limiting now that tools to enforce scope
lifetime come into play.

-------------- 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/20161016/160e9b5f/attachment-0002.sig>


More information about the Dlang-internal mailing list