在 Smalltalk 中获取消息的发件人 [英] Get sender of a message in Smalltalk

查看:57
本文介绍了在 Smalltalk 中获取消息的发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种实用的方法可以在 Smalltalk 中在不手动将 self 作为参数传递的情况下获取消息的发送者?

Is there a practical way to get the sender of a message in Smalltalk without manually passing self as a Parameter?

更具体地说:我想向传递给我的类的 ID 添加一个特定于类的前缀,因此如果 ClassA 发送(在类端)

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 应该在内部将 'myId' 视为 'ClassB-myId' 或类似的东西.

ClassB should internally treat 'myId' as 'ClassB-myId' or something similar.

我已经用一个必须是 self 的附加参数实现了这一点

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 发送者接收者

  1. thisContext 回答当前堆栈帧
  2. sender 响应父堆栈帧
  3. receiver 应答栈帧的接收者
  1. thisContext answers the current stack frame
  2. sender answers the parent stack frame
  3. receiver answers the receiver of the stack frame

这应该适用于 Pharo、VisualWorks 和 GemStone.其他 Smalltalk 可能使用不同的方法名称.

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

这篇关于在 Smalltalk 中获取消息的发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆