On Wednesday, 1 October 2014 at 15:48:39 UTC, Oren Tirosh wrote:
On Tuesday, 30 September 2014 at 19:10:19 UTC, Marc Schütz wrote:
(I'll try to make a sketch on how this can be implemented in another post.)

Do elaborate!

Here's an example implementation of what I have in mind (totally untested and won't compile because of `scope`):

http://wiki.dlang.org/User:Schuetzm/RC,_Owned_and_allocators

This is just a sketch to explain the general idea. Some things probably won't work as implemented, especially the disable postblit and opAssign() of Owned!T. I think it needs to implement implicit moving, otherwise one would have to call `release()` everywhere.

As in the other post, the function that produces the value returns Owned!T. The types don't require @unique however (although integration with DMD's idea of unique would still be useful).

Because of auto-borrowing via alias this, Owned!T and RC!T both can pass their payloads to functions that accept them by `scope`. The ref-count is not touched for borrowing.

Usage examples:

    Owned!string setExtension(in char[] path, in char[] ext);

    void saveFileAs(in char[] name) {
        import std.path: setExtension;
        import std.file: write;
        name.                    // scope const(char[])
            setExtension("txt"). // Owned!string
            write(data);
    }

    RC!string[] stringList;

    void addToGlobalList(scope RC!string s) {
        stringList ~= s;    // increments ref-count
    }

    RC!string foo;
    addToGlobalList(foo);   // borrowing doesn't change ref-count

    auto newFileName = "hello-world".setExtension("txt");
    auto tmp1 = newFileName;       // ERROR: cannot copy
    scope tmp2 = newFileName;      // OK, borrowing
    foo = newFileName;             // ERROR: cannot copy
    foo = newFileName.release();   // OK, move
    auto bar = newFileName.toRC(); // ditto

Reply via email to