如何获取协议中的所有方法?
如何获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Pharo 中,您要查找的方法是
ClassDescription>>allMethodsInCategory:
:要查找类端协议中的方法,只需发送到元类即可:
您可能需要考虑的另一个解决方案,不过,是使用编译指示来标记您的方法。有关该方法的详细信息,请参阅
Pragma
类的注释。它的优点是其他包可以自由添加属于您的组的方法(需要位于 * 协议中),并且编译指示也可以用于存储其他元数据(例如评估顺序)。注意。 选择器
allMethodsInCategory:
已在 Pharo 3.0 中弃用,后来改用allSelectorsInProtocol:
In Pharo, the method you're looking for is
ClassDescription>>allMethodsInCategory:
:To find methods in a class-side protocol, just send to the metaclass instead:
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 ofallSelectorsInProtocol: