在 Smalltalk 中获取消息的发件人

发布于 2024-08-17 08:44:58 字数 391 浏览 4 评论 0原文

有没有一种实用的方法可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-08-24 08:44:58

您可以在执行堆栈上使用反射工具:

thisContext 发送方接收方

  1. thisContext 应答当前堆栈帧
  2. sender 应答父堆栈帧
  3. receive 应答堆栈帧的接收者

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文