D 2015/2016 Vision?
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Tue Oct 6 10:20:38 PDT 2015
On Tuesday, 6 October 2015 at 17:03:07 UTC, bitwise wrote:
> On Tuesday, 6 October 2015 at 06:45:47 UTC, Jonathan M Davis
> wrote:
>> They're not the same thing at all. scoped is supposed to put
>> the class on the stack, not the heap. And it's not
>> ref-counted. It's so that you can create a class object in
>> place, use it, and throw it away without doing any heap
>> allocation. Essentially, it allows you to use a class as if it
>> were a non-copyable struct. Even if we end up with
>> ref-counting supported in the language, it doesn't obviate the
>> need for scoped classes. They're for different use cases.
> For my purposes, they are pretty much the same.
>
> So again, I'll paste the same example:
>
> class Texture { }
> class Texture2D : Texture {
> this() { /* load texture... */ }
> ~this { /* free texture */ } // OOPS, when, if ever,
> will this be called?
> }
>
> Memory is not only thing that has to be cleaned up.
Well, they might seem the same when you only look at that part,
but they won't both solve your problem, depending on what you're
trying to do.
But in general, at this point, with D, if you want deterministic
destruction, then you use structs. Classes are not the
appropriate place for it. If they were ref-counted, then they
could be, but as long as they're not, then classes are not the
place to have stuff that cares about deterministic destruction.
And if you're stuck with stuff in classes that do care about
deterministic destruction, then you have to use the sort of
solutions that C# and Java use where you don't rely on the
destructor/finalizer to clean anything up except for the cases
where you screw up and forget to manually call the function that
does the cleanup.
I expect that we'll get ref-counting for classes at some point,
since while ref-counting with structs works, as I understand it,
it does have some holes that make it less than ideal (but not
necessarily unusable). And for some reason, IIRC, RefCounted
doesn't work with classes, so you'd be forced to write your own
ref-counted wrapper. It can certainly be done though.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list