Re: How Can an Object Read its Own Name

Brian Casiello <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.gnu.general
Message-ID <[email protected]>
I'm a Smalltalk newbie, but I'll give this a shot, and hopefully learn 
something!

Consider

     Foo := Bar new .
     Sna := Foo .

Foo and Sna are two different names for the exact same object. Which one 
is its unique name?

How to handle would depend on exactly what you're trying to accomplish. 
One possibility is to give the object a unique name when it is 
constructed using an instance variable. You'd do something like

     Foo := Bar newWithUniqueName: 'Kilroy' .

The name would carry along with the object, no matter what variable(s) 
it happens to be assigned to. It could even be part of a collection, not 
assigned to a single named variable at all, but the object would still 
know its own name.

If that doesn't meet your requirements, you really need to know the name 
of the (a) variable that the object is assigned to, I'm afraid that's 
beyond me.

HTH,
Brian

On 1/18/2021 9:09 PM, Gary Highberger wrote:
> Hi Everyone,
>
> I'd like for an object to be able to send a message with it's unique name
> as an argument.
>
> For example:
> The Foo instance of the class Bar sends a message with an argument of Foo.
>
> The Sna instance of the class Bar sends a message with an argument of Sna.
>
> Foo := Bar new .
> Sna := Bar new .
>
> I tried to use self but to no avail.
>
> Many thanks,
>
> Gary
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.