[Dlang-study] [lifetime] Few root decisions to take on RC classes
Marc Schütz
schuetzm at gmx.net
Fri Nov 6 04:45:20 PST 2015
On Friday, 6 November 2015 at 02:44:39 UTC, Michel Fortin wrote:
> But how do you prevent the payload from being leaked in a way
> that it outlives the droppable? For instance, someone might
> want to log the exception message and put it into a queue for a
> separate logging thread to process. You're giving raw access to
> the string, so it's very easy mess things up if you are not
> careful.
Well, that requires borrowing/sealed references:
struct Droppable(T) {
private T payload;
void delegate(ref T object) dropper;
ref T borrow() return { return payload; }
alias borrow this;
@disabled this(this);
~this() {
if(dropper) dropper(payload);
}
}
But that's a separate topic. My point was that this is a
mechanism to abstract away from specific ownership schemes
without making the ownership part of the type (i.e. `Exception`
can be used as-is no matter whether the message if garbage
collected or refcounted or static).
More information about the Dlang-study
mailing list