[英]How can I get all the methods in a Protocol?
How can I get a collection of all the (class) methods in a given protocol in smalltalk/squeak/pharo? 如何在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. 谢谢。
In Pharo, the method you're looking for is ClassDescription>>allMethodsInCategory:
: 在Pharo中,您正在寻找的方法是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. 但是,您可能需要考虑的另一个解决方案是使用pragma标记您的方法。 See the comment on the Pragma
class for details of that approach. 有关该方法的详细信息,请参阅Pragma
类的注释。 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). 它的优点是其他包可以自由添加属于您的组的方法(需要在*协议中),并且该pragma也可以用于存储其他元数据(例如,评估顺序)。
NB. NB。 The selector allMethodsInCategory:
has been deprecated in Pharo 3.0 and later in favor of allSelectorsInProtocol:
选择器allMethodsInCategory:
已在Pharo 3.0及更高版本中弃用,支持allSelectorsInProtocol:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.