FOOP destructors?

For the Compleat Fan

FOOP destructors?

Postby ryuo » Sat Oct 11, 2014 11:09 pm

I've been wondering if it would be a good idea for FOOP objects to be able to define a destructor function. The constructor for FOOP is normally the same name as the context / class of the object. Could the destructor be defined in the same context / class and have the same name as the context / class, but prefixed with a ~ like it would be in C++? I was thinking it would be nice to be able to hook resource cleanup into a destructor or similar function. I can see this being useful to cleanup memory allocated by functions mapped from the FFI. Perhaps it could be called before newLISP frees the cell's memory. The only problem I can think of is when there are duplicates of the object floating around. It would mean that the destructor should only be called when the last copy goes out of scope. Is this in any way practical to implement, Lutz? Otherwise I am happy to continue using manual cleanup, as this is the C way. But having a way to define how to cleanup resources automatically would be nice. Thank you.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: FOOP destructors?

Postby Lutz » Sun Oct 12, 2014 7:30 am

A FOOP object is a list where the first member is symbol identifying the class of the object. That list could live anywhere, e.g. assigned to a symbol or as a submember of another list structure. Destroying all objects constructed by a specific FOOP class would mean walking all cell memory, which is very inefficient. There is no single reference point in the system to all the objects created.

But you could improve the manual cleanup by storing all objects of a class in a list referenced by a symbol owned by the FOOP class it belongs too.

Currently only functions allocated through the simple FFI can be destroyed and there memory reclaimed. Functions imported via the extended FFI using libffi, can currently not be destroyed. In general, primitives and imported functions are seen as something constant in newLISP which live through the entire life of the program.
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: FOOP destructors?

Postby TedWalther » Wed Dec 07, 2016 5:34 am

In my /dev/urandom function defined over in this thread viewtopic.php?f=16&t=4864 it would be useful to have a destructor. Purpose of destructor would be to do (close fd) to close the file descriptor.

But then I thought about it; how many "random number generator" objects would you be creating and destroying... this use case is contrived. But having constructor and destructor functions would be useful for things like filehandles that need to be opened and closed, network connections, database connections, etc. That is, transient things that take up memory and resources.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: FOOP destructors?

Postby Astrobe » Sun Mar 19, 2017 1:52 pm

FOOP. The F should be a P. Every time I try to use it for something more object-y than glorified structures, it falls flat on its face (and even as glorified structures, it's not really convenient out-of-the-box).

I'd rather use contexts for prototype-based objects.

Kind of what the /dev/urandom example actually is.

But of course, the concept of destructor doesn't make sense in this case, because the "object" lives forever. Unless... You wipe it with delete?
The only point of doing that would be to reuse the symbol (eg that object is caught in a loop). So maybe having delete to call the local definition of delete in the context being deleted (if present; otherwise do the usual thing) would make sense. Conversely new could do the same thing (aka call what would be the copy constructor).

But all this can be defined in NewLisp itself, I think. Just call them "clone" and "destroy" instead of "new" and "delete").
Astrobe
 
Posts: 38
Joined: Mon Jan 11, 2010 9:41 pm

Re: FOOP destructors?

Postby m i c h a e l » Mon Mar 20, 2017 3:41 am

FOOP. The F should be a P.

:-)


FOOP was never meant to be a complete object-oriented system. In fact, without references in FOOP, it’s not possible to write complex OO simulations. Still, it made working in newLISP (a functional programming language) more natural for someone who thinks in OO terms.

m i c h a e l
m i c h a e l
 
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA


Return to Anything else we might add?

Who is online

Users browsing this forum: No registered users and 3 guests

cron