在 Smalltalk 中获取消息的发件人
有没有一种实用的方法可以在 Smalltalk 中获取消息的发送者而无需手动传递 self 作为参数?
更具体地说:我想向传递给我的类的 ID 添加一个特定于类的前缀,因此如果 ClassA 发送(在类端)
ClassB doSomethingWith: 'myId'.
ClassB 应该在内部将“myId”视为“ClassB-myId”或类似的内容。
我已经用一个附加参数实现了这个,该参数必须是 self,
ClassB doSomethingWith: 'myId' for: self.
但如果有一个没有这种显式发送 self 的解决方案,我会非常高兴。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在执行堆栈上使用反射工具:
thisContext 发送方接收方
thisContext
应答当前堆栈帧sender
应答父堆栈帧receive
应答堆栈帧的接收者这应该适用于 Pharo、VisualWorks 和 GemStone。其他 Smalltalk 可能使用不同的方法名称。
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 frameThis should work in Pharo, VisualWorks and GemStone. Other Smalltalk might use different method names.