www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are scope class useful ?

reply #ponce <nospam nospam.com> writes:
When I started D, it was possible to define a scope class like this.

scope class Something
{
// blah
}

An instance declaration would then _require_ the scope storage class.


{
    scope Something myVar;
   // do something with Something
}

Is there a use case for such a feature.?
Jan 05 2010
parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

#ponce wrote:
 When I started D, it was possible to define a scope class like this.
 
 scope class Something
 {
 // blah
 }
 
 An instance declaration would then _require_ the scope storage class.
 
 
 {
     scope Something myVar;
    // do something with Something
 }
 
 Is there a use case for such a feature.?

Yes. One of the uses is when you are wrapping some sort of OS resource. I've got a scope class which I use for file access; stops you accidentally leaving the file open & locked. I also use scope classes for storing intermediate results in complex algorithms when I want deterministic collection. Use scope classes and it all gets cleaned up when the algorithm finishes. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLQ4cJT9LetA9XoXwRAsv8AJ0TiP7Qy4kOKf0WmWPuyYv5qtzsNACeOoCH sBUmQ67xWzjrSMB27o149i4= =hGvN -----END PGP SIGNATURE-----
Jan 05 2010
next sibling parent #ponce <nospam nospam.com> writes:
 Yes. One of the uses is when you are wrapping some sort of OS resource.
 
 I've got a scope class which I use for file access;
 stops you accidentally leaving the file open & locked.
 
 I also use scope classes for storing intermediate results in complex
 algorithms when I want deterministic collection. Use scope classes
 and it all gets cleaned up when the algorithm finishes.
 

OK, makes sense now. Neat feature indeed.
Jan 06 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
div0:
 I also use scope classes for storing intermediate results in complex
 algorithms when I want deterministic collection. Use scope classes
 and it all gets cleaned up when the algorithm finishes.

Currently scopes objects can be useful as a performance optimization too. They (when possible) get allocated on the stack, and avoiding heap allocations in D is currently very advantageous because the GC is currently slow (compared to modern GCs you can find in Java). And time ago I've said to LDC developers that there is another way to further optimize the deallocation of such stack-allocated objects (improving the deallocation code), so in future their performance will hopefully further improve a little. Bye, bearophile
Jan 07 2010
parent bearophile <bearophileHUGS lycos.com> writes:
 Currently scopes objects can...

What I have said is about scoped objects, while you were talking about scoped classes, a feature I didn't even remember about. Sorry :-) Bye, bearophile
Jan 08 2010