Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 501
jdinunzio Avatar asked Jun 07 '10 15:06

jdinunzio


People also ask

What is optional methods in Swift protocol?

Swift protocols on their side do not allow optional methods. But if you are making an app for macOS, iOS, tvOS or watchOS you can add the @objc keyword at the beginning of the implementation of your protocol and add @objc follow by optional keyword before each methods you want to be optional.

How many protocols can a Swift class adopt?

Since classes, structures and, enums can conform to more than one protocol, they can take the default implementation of multiple protocols.

Can Swift protocols have properties?

A protocol can require any conforming type to provide an instance property or type property with a particular name and type. The protocol doesn't specify whether the property should be a stored property or a computed property—it only specifies the required property name and type.

What's the difference between a protocol and a class in Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.


1 Answers

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:

like image 148
Ash Wilson Avatar answered Nov 06 '22 04:11

Ash Wilson