Auto objects and scope

Boris Kolar boris.kolar at globera.com
Fri Nov 17 04:50:04 PST 2006


== Quote from Bill Baxter (dnewsgroup at billbaxter.com)'s article
> I think some sort of reference-counting construct is needed for the type
> of cases your example illustrates.  For things like File objects, you
> want to be able to return them, but you want them to be cleaned up
> immediately when no longer in use.  'scope' is too limited I think.
> I'm not sure how to do it though.  It's not clear how to do in a clean
> way as a library (like a shared_ptr!()) without an overloadable opAssign.
> --bb

I agree completely. Here is a different solution for a similar problem:
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=44163

Ideally, even cases like this should have deterministic finalization:

  void test() {
    getFile().read(buffer);
    // the File instance returned by getFile is destroyed here
    // (assuming it's not used outside this scope, of course)
  }

The easiest way IMHO is intruduction of value classes (which are immutable)
and using reference counting for deterministic finalization. Note that
(immutable) value classes can never form cyclic references, so this
technique would work well.

Potentially, value classes offer additional benefits by providing better
opportunities for compile-time optimization (multithreading, caching,...).



More information about the Digitalmars-d mailing list