On Thursday, 23 October 2014 at 13:03:08 UTC, Marco Leise wrote:
Am Thu, 23 Oct 2014 12:15:13 +0000
schrieb "Marc Schütz" <schue...@gmx.net>:
Yet another use case for borrowing.
Cool, how does it keep the original struct alive though, if it
isn't stored anywhere? Or will it error out when you attempt
to use the dangling pointer to the object?
It needs to error out. Artificial life support for the original
object would be too much magic for my taste.
struct Scoped(T) if(is(T == class)) {
private T _payload;
// <insert constructor and destructor here>
scope!this(T) borrow() { return _payload; }
alias borrow this;
}
void foo() {
A a = scoped!A();
// ERROR: cannot assign Scope!A to A
scope b = scoped!A();
// ERROR: local `b` outlives temporary
}