如何获取协议中的所有方法?

发布于 2024-09-04 08:44:32 字数 135 浏览 3 评论 0原文

如何获取 Smalltalk/squeak/pharo 中给定协议中所有(类)方法的集合?

我正在尝试收集一组方法返回的值。我不想将方法存储在实例或类变量中。所以我想我可以将它们添加到协议中并以这种方式“标记”它们。

谢谢。

How can I get a collection of all the (class) methods in a given protocol in smalltalk/squeak/pharo?

I'm trying to collect the values returned by a group of methods. I don't want to have to store the methods in an instance or class variable. So I though I could add them to a protocol and in this way to "mark" them.

Thanks.

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

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

发布评论

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

评论(1

看海 2024-09-11 08:44:32

在 Pharo 中,您要查找的方法是 ClassDescription>>allMethodsInCategory:

| selectors |
selectors := MyClass allMethodsInCategory: #'protocol name'.

要查找类端协议中的方法,只需发送到元类即可:

selectors := MyClass class allMethodsInCategory: #'protocol name'.

您可能需要考虑的另一个解决方案,不过,是使用编译指示来标记您的方法。有关该方法的详细信息,请参阅 Pragma 类的注释。它的优点是其他包可以自由添加属于您的组的方法(需要位于 * 协议中),并且编译指示也可以用于存储其他元数据(例如评估顺序)。

注意。 选择器 allMethodsInCategory: 已在 Pharo 3.0 中弃用,后来改用 allSelectorsInProtocol:

In Pharo, the method you're looking for is ClassDescription>>allMethodsInCategory::

| selectors |
selectors := MyClass allMethodsInCategory: #'protocol name'.

To find methods in a class-side protocol, just send to the metaclass instead:

selectors := MyClass class allMethodsInCategory: #'protocol name'.

Another solution you might want to consider, though, is to use a pragma to mark your methods instead. See the comment on the Pragma class for details of that approach. It has the advantages that other packages can freely add methods belonging to your group (which need to be in a * protocol), and that the pragma can be used to store other metadata as well (such as an evaluation order, for example).

NB. The selector allMethodsInCategory: has been deprecated in Pharo 3.0 and later in favor of allSelectorsInProtocol:

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