digitalmars.D.learn - Cleanup class after method?
- JN (12/12) Jul 04 2018 Imagine I have a very short-lived class:
- Mr.Bingo (2/14) Jul 04 2018 https://dlang.org/phobos/std_typecons.html#scoped
- Jonathan M Davis (15/27) Jul 04 2018 The typical thing to do in that case is to use a struct, not a class, si...
- Flaze07 (4/8) Jul 04 2018 I read the proposal about -dip1000 ( not all, some ) and there is
- Jonathan M Davis (8/17) Jul 05 2018 Long term, it will be better to use the scope keyword. Short term (i.e.
Imagine I have a very short-lived class:
void print(File f)
{
PrinterManager pm = new PrinterManager();
pm.print(f);
}
My understanding is that PrinterManager will be GC allocated, and
when it goes out of scope, the GC will possibly clean it up at
some point in the future. But I know that this class won't be
used anywhere, I want to clean it up right now so that GC doesn't
waste time later. In C++ it'd be handled by RAII, pm would be a
unique_ptr<PrinterManager>. How to do it in D?
Jul 04 2018
On Wednesday, 4 July 2018 at 15:47:25 UTC, JN wrote:
Imagine I have a very short-lived class:
void print(File f)
{
PrinterManager pm = new PrinterManager();
pm.print(f);
}
My understanding is that PrinterManager will be GC allocated,
and when it goes out of scope, the GC will possibly clean it up
at some point in the future. But I know that this class won't
be used anywhere, I want to clean it up right now so that GC
doesn't waste time later. In C++ it'd be handled by RAII, pm
would be a unique_ptr<PrinterManager>. How to do it in D?
https://dlang.org/phobos/std_typecons.html#scoped
Jul 04 2018
On Wednesday, July 04, 2018 15:47:25 JN via Digitalmars-d-learn wrote:
Imagine I have a very short-lived class:
void print(File f)
{
PrinterManager pm = new PrinterManager();
pm.print(f);
}
My understanding is that PrinterManager will be GC allocated, and
when it goes out of scope, the GC will possibly clean it up at
some point in the future. But I know that this class won't be
used anywhere, I want to clean it up right now so that GC doesn't
waste time later. In C++ it'd be handled by RAII, pm would be a
unique_ptr<PrinterManager>. How to do it in D?
The typical thing to do in that case is to use a struct, not a class, since
structs have RAII, and they don't need to be allocated on the heap. However,
if you're stuck with a class, you can use scope. e.g.
scope pm = new PrintManager;
In that case, the class will actually be allocated on the stack instead.
However, I would point out that until -dip1000 becomes the normal behavior,
using scope like this is unsafe, because you have serious problems if any
reference to the class object escapes, since it's stack-allocated instead of
heap-allocated, and if any reference to it escapes, then it's referring to
an invalid object. If you're careful, it's fine, but it is a risk, and
regardless, using a struct is preferable if it's possible.
-dip1000 fully implements scope so that it verifies that no reference
escapes, but it's not ready yet, let alone the default behavior.
- Jonathan M Davis
Jul 04 2018
On Wednesday, 4 July 2018 at 16:02:25 UTC, Jonathan M Davis wrote:-dip1000 fully implements scope so that it verifies that no reference escapes, but it's not ready yet, let alone the default behavior. - Jonathan M DavisI read the proposal about -dip1000 ( not all, some ) and there is also a scoped! template in std.typecons, so...is it better to use the scope keyword, or scoped! template
Jul 04 2018
On Thursday, July 05, 2018 05:47:20 Flaze07 via Digitalmars-d-learn wrote:On Wednesday, 4 July 2018 at 16:02:25 UTC, Jonathan M Davis wrote:Long term, it will be better to use the scope keyword. Short term (i.e. until -dip1000 is the normal behavior or at least ready to use), it may be better to use scoped, since I think that the wrapper protects you at least somewhat - though the main reason that it was originally introduced is that scope on classes was going to deprecated, because we didn't want something that unsafe to be a keyword, but DIP 1000 is changing that. - Jonathan M Davis-dip1000 fully implements scope so that it verifies that no reference escapes, but it's not ready yet, let alone the default behavior. - Jonathan M DavisI read the proposal about -dip1000 ( not all, some ) and there is also a scoped! template in std.typecons, so...is it better to use the scope keyword, or scoped! template
Jul 05 2018









Mr.Bingo <Bingo Namo.com> 