This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Saying goodbye is not hard
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
Two evenings ago, Boris Popov showed up in the #smalltalk irc channel, and we talked about ephemerons. I had forgotten how these things worked, but once upon a time, I knew when I wrote Weaklings. It's pretty straightforward. When you create a class whose indexType: is #ephemeron, the first ivar slot is weak. And when the object you're holding is no longer referenced strongly, you get a sent a mourn message, which by default does nothing. So you can choose to let go of it too. If you don't it won't actually go away. The Weaklings package has two variants on this pattern: one a "ValueHolder" like thing, and the other a Proxy. I've used the asWeakling quite a bit in some of our code. It's a great way to have a process loop which runs for the life of an object, but does not contribute to the object staying around:
What Boris wanted though was some of the "instance based finalization" that ephemerons purport to offer. The thing about an ephemeron is that it doesn't make the weak thing itself have anything special happen to it when it's being gotten rid of; rather the thing that holds it weakly gets a chance to know about it. Boris had a DLLCC reference which needed to be "managed" the associated library when it was ready to go away. This got me thinking that it would be nice if there were a simple way of plugging in "finalization actions" for these weakling things.
So I sat down this morning and wrote some tests, and then made some code work. There's a new version of Weaklings in the OR, as well as a WeaklingsTests. The basic idea is to add a 'lastRites:' message to Weakling. It's a single arg block that can do whatever you want with the object before it goes away. In this way, should Boris so choose, he can do something like:
He'll have to use value to get at the actual pointer inside the weakling. But it's all just automatic now. He could use a WeakProxy to hide the value thing: