Determining the sender in a method

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

Determining the sender in a method

Rob Rothwell
Is there a way to determine "who" called "me" in a method?  Or would doing this just be bad form?

I have a method in an object that creates another object FOR the sender.  Rather than pass a parameter, it seemed like it would be nice to just "know" who was asking for the component.

For example, rather than

MyObject>>doSomething
     WidgetBuilderSubclass buildComponentForObject: self anObject.

it would be nice to just

MyObject>>doSomething
     WidgetBuilderSubclass buildComponent

and have

WidgetBuilderSubclass>>buildComponent
     | obj widget |
     obj := self thisMessageSender anObject.
     ...
     ^widget

I am probably asking a question that shows just how much I don't understand "the Smalltalk way!"

Thanks,

Rob


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Determining the sender in a method

Randal L. Schwartz
>>>>> "Rob" == Rob Rothwell <[hidden email]> writes:

Rob> Is there a way to determine "who" called "me" in a method?

Yes, see the methods of "thisContext", a special variable
that represents the currently executing context.  The methods
are defined in MethodContext, on which a protocol-browse will reveal
some really interesting (and horribly introspecting) methods, such
as "thisContext sender".

Rob> Or would doing this just be bad form?

Yes.  While you *can* do a lot of magic like this, it's probably
best if you avoid it for now.  A typical pattern is to pass self:


MyObject>>doSomething
  ^self otherClass makeSomethingOn: self.

Also note: don't hardwire a helper class name in your methods.  Use
a method call to get it, so you'd say:

MyObject>>otherClass
  ^TheOtherClass

If you hardwire it, it'll make your class much harder to subclass.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Determining the sender in a method

Rob Rothwell
On Sun, May 25, 2008 at 12:59 AM, Randal L. Schwartz <[hidden email]> wrote:
Rob> Or would doing this just be bad form?

Yes.  While you *can* do a lot of magic like this, it's probably
best if you avoid it for now.  A typical pattern is to pass self:


MyObject>>doSomething
 ^self otherClass makeSomethingOn: self.

Thanks...this is almost what I did, lacking the "bad form" MethodContext introspecting methods you described.  Although I admit I hardwired it as below, but will rectify that soon enough!

Also note: don't hardwire a helper class name in your methods.  Use
a method call to get it, so you'd say:

MyObject>>otherClass
 ^TheOtherClass

If you hardwire it, it'll make your class much harder to subclass.

Which is what I am doing (subclassing).  This "trivial" example shows I am making progress but still somewhat bound by the laws of non-object programming!

Thanks again,

Rob

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners