Is there a practical way to get the sender of a message in Smalltalk without manually passing self as a Parameter?
To be more concrete: I want to add a class specific prefix to an ID that gets passed to my class, so if ClassA sends (on class side)
ClassB doSomethingWith: 'myId'.
开发者_运维百科
ClassB should internally treat 'myId' as 'ClassB-myId' or something similar.
I have implemented this with an additional parameter which has to be self
ClassB doSomethingWith: 'myId' for: self.
but I would be very glad if there is a solution without this explicit send of self.
You can use the reflective facilities on the execution stack:
thisContext sender receiver
thisContext
answers the current stack framesender
answers the parent stack framereceiver
answers the receiver of the stack frame
This should work in Pharo, VisualWorks and GemStone. Other Smalltalk might use different method names.
精彩评论