Is there an implementation of class interfaces for Smalltalk?

C # classes can have interfaces that can have multiple implementations. How do you do this in smalltalk?

+4
source share
4 answers

First of all, you usually do not need interfaces, because if an object implements the same messages as others, it can replace it. In Java and C #, you cannot do this if they are not in the same hierarchy, so you need interfaces.

  • In (all) Smalltalk there are protocols (categories of methods) that are shared for informal grouping purposes.
  • Pharo Smalltalk has Traits. At first they look like interfaces, but they can also provide an implementation.
+12
source

After a discussion today with my colleague, it seems to me that the answer to any class can be considered an interface, because any class can be transmitted in a message to any other class.

Any number of classes in smalltalk can respond to the same message, so you do not need interfaces in accordance with C # and java.

+5
source

As Lucas said, in most cases they do not need you. Mostly because in order to achieve polymorphism, the only thing you need is to implement the same message. There is no need to define a common type for them.

On the other hand, sometimes from my point of view you need interfaces. Basically, when you have a show contract, or when you have an abstract abstract superclass. This is very common when developing a framework. Take, for example, a registrar or serializer. In this case, you may need to define the required methods that the serializer must implement. Then you can create an abstract superclass with all the methods implemented this way:

LoggerIterface >> log: anObject self shouldBeImplemented LoggerIterface >> reset self shouldBeImplemented 

Etc ... therefore, by checking this class, you can now use the methods that should be executed by the user of this object. Note that #shouldBeImplemented is implemented in Object with something like this (in Pharo Smalltalk):

 Object >> shouldBeImplemented "Announce that this message should be implemented" self error: 'This message should be implemented' 

But, as you can see, this is just an agreement, it is not imposed by the language itself.

Greetings

Mariano

+5
source

Even if itโ€™s not called โ€œInterfaces,โ€ Dolphin Smalltalk (http://www.object-arts.com/) provides a function called โ€œProtocols,โ€ which are first class objects.

Each protocol defines a set of selectors (method names), and you can check whether the class matches a specific protocol or not:

 conformsToProtocol: protocol "Answer whether the receiver conforms to the named <MethodProtocol>." 

As a result, you have a formal / specific set of method names, and you can check if a specific object can be used in the context of the protocol. In addition, the class browser displays a list of protocols to which the selected class belongs.

And there is a protocol browser, so you can examine each protocol and view, within the whole system, which classes correspond to them.

To summarize: Interfaces are not needed in Smalltalk, at least to implement polymorphism. However, some Smalltalk dialects provide varying degrees of protocol support, which are analog interfaces, but for dynamic languages.

+4
source

Source: https://habr.com/ru/post/1342220/


All Articles