[Newbies] Determining the sender in a method

Randal L. Schwartz merlyn at stonehenge.com
Sun May 25 04:59:55 UTC 2008


>>>>> "Rob" == Rob Rothwell <r.j.rothwell at gmail.com> 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
<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