reference - Idiomatic way to determine if an object has been destroyed -
i've been trying find better way accomplish determining if particular object has been destroyed (destroy(...)
). way i've been doing so:
class c { bool valid = false; this(){ valid = true; } }
then do:
c c = new c; c.valid.writeln // true destroy(c); c.valid.writeln // false if(c !is null && !c.valid) c = null;
i don't see wrong (perhaps can tell me else wrong it) other takes memory , requires putting valid = true;
in each constructor (and ugly because uses variable destroyed object). best case, of course, have magical function can tell if object valid or not valid(c); // true / false
.
so question if there standard way determine if object has been destroyed (like, gc hasn't collected memory location , valid object sitting in spot without reference vtable) , pointer virtually dangling? if there isn't way secondary question: method dangerous in foreseeable way?
previously, made sure each reference object -> b there reference b -> a, , upon applying destroy a's destructor nullified b's reference a. never had check if destroyed. tedious , time consuming when want add new type of reference because have modify both destroyable class (a) , referencing class (b). theoretically, having determinable cycle in reference graph of program (or that); potentially interesting subject.
sorry in advance if i'm being idiot.
by default d use gc deal reference types (class in case). means if use defaults, can't expect deterministic object destruction.
jonathan explained nicely in thread: usage preference between struct , class in d language
if need deterministic destruction - use structs. method described reminds me of scala's option type.
Comments
Post a Comment