Get sender of a message in Smalltalk

拥有回忆 提交于 2019-12-08 06:17:44

问题


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.


回答1:


You can use the reflective facilities on the execution stack:

thisContext sender receiver

  1. thisContext answers the current stack frame
  2. sender answers the parent stack frame
  3. receiver answers the receiver of the stack frame

This should work in Pharo, VisualWorks and GemStone. Other Smalltalk might use different method names.



来源:https://stackoverflow.com/questions/2028204/get-sender-of-a-message-in-smalltalk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!