Safe Regions with Deterministic Destruction

Per Nordlöw per.nordlow at gmail.com
Mon Oct 26 19:47:57 UTC 2020


On Monday, 26 October 2020 at 14:27:49 UTC, Per Nordlöw wrote:
> The typical use case I see is a tree `T` that `emplace`s all 
> its nodes in a region `R` where `R`'s lifetime and, in turn, 
> `T`s nodes are all deterministically bound to `T`s lifetime.

Here's the essence of what I had in mind:

struct Tree(Node) if (is(Node == class))
{
@safe:
     Node root() return scope pure nothrow @nogc { return _root; }
     this(uint dummy) @trusted
     {
         import core.lifetime : emplace;
         _root = emplace!Node(_store);
     }
     private Node _root;
     private void[__traits(classInstanceSize, Node)] _store; // 
replaced with a region
}

class C { this() {} int x; }

@safe pure unittest
{
     C f() {       Tree!C t; return t.root; } // should error?
     C g() { scope Tree!C t; return t.root; } // errors
}

What's the reasoning behind disallowing g() but not f()? Lack of 
`scope`-inference in the compiler? Calling f() will result in a 
memory error aswell.


More information about the Digitalmars-d mailing list