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
"Rob" == Rob Rothwell r.j.rothwell@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.
On Sun, May 25, 2008 at 12:59 AM, Randal L. Schwartz merlyn@stonehenge.com 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@lists.squeakfoundation.org