Destructor semantics

Michel Fortin michel.fortin at michelf.com
Wed Aug 11 13:49:00 PDT 2010


On 2010-08-11 16:14:56 -0400, Don <nospam at nospam.com> said:

> Michel Fortin wrote:
>> Sure, and now you can't use std.containers.Array as a member in a 
>> Class, neither std.stdio.File, etc. Is there something left classes can 
>> do?
> 
> As far as I can tell, the use cases for finalizers are very limited. To 
> me, destructors don't make any sense unless they are deterministic.
> For example, I think having a File class is probably a bug. You may be 
> on a system which has no limit on the number of file handles, but 
> generally, if you need resource management, you need a guarantee that 
> the destructor will be called.
> 
> If a file handle is stored as part of a class, with a destructor that 
> closes the file, the file might never get closed. So I don't think it's 
> unreasonable to say that a struct with a destructor cannot be a member 
> of a class.

Yes, this might make sense for a file handle. But not all struct 
destructors handle such kind of resource. The other example was 
std.containers.Array. Should its use be disallowed in a class?


> Personally I think destructors should be restricted to structs and 
> scope classes. I suspect that some form of registration with the gc 
> could do the job of finalizers.
> 
> Destructor <=> deterministic destruction.

Having a clear distinction between destructors and finalizers certainly 
makes sense.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list