Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 05, 2010 Are scope class useful ? | ||||
---|---|---|---|---|
| ||||
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.? |
January 05, 2010 Re: Are scope class useful ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to #ponce Attachments: | #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 |
January 06, 2010 Re: Are scope class useful ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to div0 | > 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.
|
January 08, 2010 Re: Are scope class useful ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to div0 | 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
|
January 08, 2010 Re: Are scope class useful ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > 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
|
Copyright © 1999-2021 by the D Language Foundation