How Can an Object Read its Own Name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How Can an Object Read its Own Name

highbeg
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
Reply | Threaded
Open this post in threaded view
|

Re: How Can an Object Read its Own Name

Brian Casiello
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