在Squeak中,如何包装每个方法send?

发布于 2024-07-21 07:16:37 字数 327 浏览 6 评论 0原文

我创建了一个类,在该类中我有一个方法“sendMessage: to: withArgs:”,它接收一个对象、一条消息和一个参数数组。 该方法用于向对象发送消息并执行一些算法。 要使用此方法,我必须创建我创建的类的实例 x 并执行类似 x sendMessage: '+' to: '7' withArgs: '#(5)' 的操作。 将消息“+”发送到带有参数 5 的对象 7 的结果,加上我的算法所做的一些事情。 但我想要的是该算法将在每个方法调用中使用,这意味着 7+5 将调用我的“sendMessage: to: withArgs:”。 我怎样才能做到这一点? 或者至少,发送到每个对象的每个方法中是否都调用了某些内容?

I created a class, and in that class I have a method 'sendMessage: to: withArgs:' which recieves an object, a message and an array of arguments.
The method is used to send messages to object and perform some algorithm.
To use this method I have to create an instance x of the class I created and do something like x sendMessage: '+' to: '7' withArgs: '#(5)'.
The result of this sending the message '+' to the object 7 with the parameter 5, plus some stuff that my algorithm does. But what I want is that the algorithm will be used in every method call, meaning 7+5 will call my 'sendMessage: to: withArgs:'.
How can I do that? Or at least, Is there something called in each method sent to every object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

纸短情长 2024-07-28 07:16:37

这有点有趣,我们刚刚在 Squeak irc 频道中讨论过这个问题。 也许看一下 ObjectViewer。

在您的示例中,您想要拦截发送到 SmallInteger 的消息。 有趣的是,ObjectViewer 几乎适用于除 SmallInteger 之外的所有类。

因此,要拦截发送到 myObject 的消息,请执行以下操作。

创建类Intercepter,让它继承自ObjectTracer,也许吧。 将 doesNotUnderstand 更改为为您服务的内容:

doesNotUnderstand: aMessage
   "do Mojo to aMessage as you describe it"

然后,为了让您的东西继续运行,创建您的拦截器:

myIntercepter := Intercepter on: myObject.

然后

myObject become: myInterceptor.

It's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps.

In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, ObjectViewer works with very much every class BUT SmallInteger.

So, to intercept message sends to myObject, do this.

Create class Intercepter, let it inherit from ObjectTracer, perhaps. Change doesNotUnderstand to something that serves you:

doesNotUnderstand: aMessage
   "do Mojo to aMessage as you describe it"

Then, to get your stuff going, create your Intercepter:

myIntercepter := Intercepter on: myObject.

And then

myObject become: myInterceptor.
向日葵 2024-07-28 07:16:37

在 Squeak 中,请参阅 ObjectTracer 类。 类注释描述了如何使用它。 你应该能够用它来完成你所需要的,或者至少使用它作为模型。

In Squeak, see the class ObjectTracer. The class comment describes how to use it. You should be able to accomplish what you need with that, or at least using that as a model.

独行侠 2024-07-28 07:16:37

查看反射率

不幸的是,一些论文链接不起作用,而且我不记得从头到尾的确切调用,但根据需要检测代码确实很容易,甚至可以在运行时执行。 查找使用 Link 类的示例。

Have a look at the Reflectivity.

Unfortunately some of the paper links are not working, and I don't remember the exact invocation from the top of my head, but it's really easy to instrument code as you want, and even do it at runtime. Look for examples using class Link.

眼眸印温柔 2024-07-28 07:16:37

您可以使用方法包装器。 要了解方法包装器是什么,您可以查找一篇名为“救援包装器”的论文。 我认为有一个 squeak 包已经实现了方法包装器。
此外,您还可以看到在最新版本的 Pharo 中如何进行测试代码覆盖率分析,因为它使用一种方法包装器来查看在测试运行期间评估了哪些方法。
干杯,
加博托

You can use method wrappers. To see what method wrappers are you can look for a paper called "Wrappers to the rescue". I think there is a package for squeak that already implements method wrappers.
In addition, you can see how a test code coverage analysis is made in the last version of Pharo because it uses a kind of method wrapper to see what methods are evaluated during a test run.
cheers,
Gaboto

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