How can I get all the methods in a Protocol?

一笑奈何 提交于 2020-01-12 15:09:08

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/2990966/how-can-i-get-all-the-methods-in-a-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!