[Newbies] What is a "weak" reference

Randal L. Schwartz merlyn at stonehenge.com
Mon Jul 14 14:32:38 UTC 2008

>>>>> "Michael" == Michael Davies <mykdavies+squeak at gmail.com> writes:

Michael> I note that there are very few uses of any Weak* classes in the image,
Michael> so it's likely that there is a better way of implementing what you're
Michael> aiming for; eg the child could drop its direct parent reference, and
Michael> do something like

Child> parent
Michael>     ^ Parent allInstances detect: [ :each | each children includes: self ]

Michael> (or vice versa depending on which way you're more likely to traverse
Michael> the relationship).

I'd argue though that a solution that involves #allInstances has a bit of code
smell.  For example, consider an XML node, looking for its parent.  In a given
parsed document, you'll likely have thousands of nodes, and it would be
clearly inefficient to continually find the one parent by asking all of them.
In that case though, weak refs probably weren't needed anyway, since each node
can just contain its parent.

Michael> This is just me thinking out loud though - perhaps someone else could
Michael> comment on best practice in this case. "Smalltalk Best Practice
Michael> Patterns" is very useful in answering these types of questions, but my
Michael> copy is in a box somewhere at the moment.

Absolutely.  A clearly seminal book.  My dog-eared copy is within reach, at
least when I'm in my home office.  Too bad Kent has gone off to the Java, and
now C# realm, if I heard right.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

More information about the Beginners mailing list