Question.

47 views
Skip to first unread message

Sergei Filler

unread,
Mar 31, 2020, 1:33:26 PM3/31/20
to VA Smalltalk
Variable B belongs to Object A.
How can B get the reference to object A?

Louis LaBrunda

unread,
Mar 31, 2020, 5:40:00 PM3/31/20
to VA Smalltalk
Hi Sergei,


Assuming what you are calling object A is a class of your making, you need to add an instance variable to that class calling it whatever you like, say varA.  The add setter and getter methods like varA and varA:, something like this:

varA

^varA


varA: anObject

varA
:=anObject.


This is very basic stuff, so maybe a book on Smalltalk would help.  Sorry, I can'y think of one off the top of my head.  Maybe someone else will post a recommendation.  There are also examples in the VA Smalltalk help docs.

Lou

Richard Sargent

unread,
Mar 31, 2020, 8:03:06 PM3/31/20
to VA Smalltalk
Sorry, Sergei. I had meant to send this reply earlier today, but got distracted.

On Tuesday, March 31, 2020 at 10:33:26 AM UTC-7, Sergei Filler wrote:
Variable B belongs to Object A.
How can B get the reference to object A?

The short answer is "It cannot."
A longer answer is for A to tell B that it is referenced by A.

The true answer is that using the reflection capabilities of Smalltalk, you can find reference paths. My favourite tool for VA Smalltalk is/was called The Exorcist. I don't know how well it works under recent versions, but I expect it would be fine. Instantiations has been exceptionally careful about preserving their APIs. It might be available from VAST Goodies.

Louis LaBrunda

unread,
Apr 1, 2020, 8:11:44 AM4/1/20
to VA Smalltalk
Hi Sergei,

Sorry, I read your question wrong, backwards.  I read "How can B get the reference to object A?" to be "How can A get the reference to object B?".  I'm a bit dyslectic and when I'm not careful, I do this kind of thing.  Anyway, my answer if not much different.  Object B can't know about A without being told about it one way or another.

Some objects (most don't) have an instance variable called #parent, to identify this kind of relationship.  Maybe looking at them will help.

Lou

Sergei Filler

unread,
Apr 1, 2020, 9:28:10 AM4/1/20
to VA Smalltalk
Hi Louis, Richard.

Thanks for the answers. Now I will stop researching how to do the thing in the topic :))
Just a continuation: can I get the list of instances of a class if I know the name of that class?

Wayne Johnston

unread,
Apr 1, 2020, 10:37:47 AM4/1/20
to VA Smalltalk

Hans-Martin Mosner

unread,
Apr 1, 2020, 10:58:14 AM4/1/20
to VA Smalltalk
Am Dienstag, 31. März 2020 19:33:26 UTC+2 schrieb Sergei Filler:
Variable B belongs to Object A.
How can B get the reference to object A?

It's probably best if you start by stating the actual problem that you're trying to solve, and what your level of experience with Smalltalk is. You might get more helpful answers than if people need to guess those things.

In general, Smalltalk does not have a concept of "ownership", only references. So if an object A has a reference to an object B, it does not own it, there may be other objects who also have a reference. If you're talking about variables, those are the places where references to objects are held, but they are not objects in themselves (with the exception of shared variables which are basically a level of indirection).

That said, there are methods by which you can find which objects point to a given object. These can be used do debug situations in which an object still exists although you thought it should have been garbage collected, but they are not to be used in application code.

If your application requires an object to know who references it, add an explicit instance variable. For example, if you're building a tree data structure consisting of nodes, a common pattern in Smalltalk is to have instance variables "children" and "parent" which are maintained to stay in sync, so that each node in a tree knows its own children and its parent.

Cheers,
Hans-Martin

Sergei Filler

unread,
Apr 1, 2020, 11:10:28 AM4/1/20
to VA Smalltalk
Great post, Wayne :-)

среда, 1 апреля 2020 г., 17:37:47 UTC+3 пользователь Wayne Johnston написал:
Reply all
Reply to author
Forward
0 new messages