D 2015/2016 Vision?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 6 11:43:40 PDT 2015


On Tuesday, 6 October 2015 at 18:10:42 UTC, bitwise wrote:
> On Tuesday, 6 October 2015 at 17:20:39 UTC, Jonathan M Davis 
> wrote:
> I'm not sure what else I can say. The example I posted says it 
> all, and it can't be done properly in D (or C#, but why lower 
> the bar because of their mistakes? ;)

It's a side effect of having the lifetime of an object managed by 
the GC. There's no way around that except to use something else 
like manual memory management or reference counting. In D, it's a 
good reason to use structs to manage resources like that, and 
since most objects really have no need of inheritance and have no 
business being classes, it's usually fine. But in the cases where 
you do have to use a class, it can get annoying.

> I'm not sure exactly how C# and Java handle destruction for 
> non-memory resources, but I'm guessing it's something like 
> calling GC.collect() manually every couple of seconds. If the 
> textures aren't released in the destructor, I don't really see 
> any other way to tell when they're referenced or not.
>
> Of course though, mobile devices are the new PC, and battery 
> life is very much a concern, so this is a total 
> waste...especially if I'm doing very little GC allocation 
> anyways. Also, of course, there are the performance issues.

You simply do not rely on the GC or the destruction of the object 
to free system resources. You manually call a function on the 
object to free those resources when you're done with it. In the 
case of C#, they have a construct to help with it that (IIRC) is 
something like

using(myObj)
{
} // myObj.dispose() is called when exiting this scope

In Java, you'd have no choice but to call dispose manually. And 
yes, that sucks, but it's life with a GC-managed object. The GC 
has a number of benefits to it, but it does not come without its 
costs.

Having the option to have properly ref-counted classes in 
addition to classes managed by the GC would definitely be an 
improvement, and I expect that we'll get there at some point, but 
there _are_ ways to deal with the problem in the interim, even if 
it's not ideal.

In most cases though, just don't use classes. In most cases, 
inheritance is a horrible way to write programs anyway, because 
it's _horrible_ for code reuse. It definitely has its uses, but 
I've found that I rarely need classes in D. I suspect that far 
too many folks new to D end up using classes instead of structs 
just because they're used to using classes in C++ or Java or 
whatever.

- Jonathan M Davis


More information about the Digitalmars-d mailing list