
Using MEMORY to make a "Destructor"
Quote:
>> The idea of a "destructor" is all about gracefully destroying an
>>object; however, in the ideal Eiffel world, objects are never destroyed.
>>Object destruction is an artifact of our limited non-ideal computers,
>>and is a reflection of memory management issues. Therefore, it is
>>perfectly logical to put destructor features into the MEMORY class.
>It is only logical if you limit your view to memory. There are some other
>resources, like file handles, locks, ports, etc. The idea of using a
>garbage collector may have started with memory in mind (I'm sure it has)
>but it applies to other resources as well: leave it to the runtime system
>to determine whether an object (and the resource that it encapsulates) is
>still needed or not. Reclaiming the resource for the OS is a different and
>class-specific task.
>So, given this broader view, class MEMORY is the wrong place for a
>destructor method. At least it should be renamed to something like
>RESOURCE, with a narrower class MEMORY derived from it. One can
>then derive other classes that encapsulate a resource and offer
>methods to lock and release it.
That's true. So I'd say that the idea of a destructor is useful when
there is resource freeing that is not time-critical. If it's time-
critical, it should be done with an explicit function call.
Quote:
>On a side track, I object to the notion of objects living forever
>naturally (whatever nature they have :-) At least those objects
>that represent mutable real-world entities in programs to have a
>well-defined life-cycle and will have to be (more or less explicitly)
>removed from the system at the end of this cycle. The usual problem
>is then find out who depends on this particular object, instead of
>vice versa as supported by garbage collectors, in order to propagate
>its removal properly.
I'm not sure I follow that last bit about dependencies, but in
referentially-transparent languages where objects are values rather
than entities, objects do (conceptually) live forever. For example,
when the expression 2+3 is executed, a 5 object is created and never
(conceptually) destroyed. Of course, one could argue that it was
never created either, and that it "always" existed, but the fact is
that the 5 object never goes away.
This integer example is rather trivial, but the idea can be extended
to other more complicated objects. For example, in the database
world, if I select some subset of rows from a table, then that subset
is a value rather than an entity in the sense that once I've defined
the subset I should be able to refer to it as often as I like for the
duration of the program. However, if the program determines that
no references exist at a given point, then it's safe to say that
they never will again, and the subset can be deleted from memory without
ruining the illusion that it lasts forever.
This is not an argument in favour of garbage-collection per se, but
in favour of the view that objects are never destroyed. Of course, in
a real system, this implies GC.
-PD
--
--
Patrick Doyle