[Oberon] Protocols (interfaces) in Oberon-2
Andreas Pirklbauer
andreas_pirklbauer at yahoo.com
Sun Oct 25 11:21:13 CET 2020
Protocols (sometimes called interfaces) can be added to
Oberon-2 without adding any keywords to the language.
This is one of the key differences to how it is usually defined
and implemented, e.g. in Swift [*] or in Integrated Oberon [**].
Under the new minimalistic design, what distinguishes a protocol
from an actual implementation (of the class) is that in the protocol
definition the implementations of the class methods are simply not
defined. Instead, any module that *imports* a protocol definition
can “adopt” /i.e. implement) it. See the example below.
An experimental implementation showed that if the language
is extended in *this* way, the implementation cost is minimal.
But the question is: Is it worth it? Simplicity of implementation
should of course not be a criteria for adopting a new feature.
Personally, I am rather sceptical of the usefulness of protocols.
But perhaps someone provides a good reason to adopt them.
-ap
Example:
MODULE TextProtocol; (*protocol definition*)
TYPE Text = POINTER TO TextDesc;
TextDesc = RECORD data: (*hidden text data*) END ;
PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: LONGINT);
PROCEDURE (t: Text) Delete (from, to: LONGINT);
PROCEDURE (t: Text) Length (): LONGINT;
END TextProtocol;
MODULE Text1; (*one implementation of the Text protocol*)
IMPORT TextProtocol;
TYPE Text = POINTER TO TextDesc;
TextDesc = RECORD (TextProtocol) END ; (*this means: “implements TextProtocol"*)
PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: LONGINT);
BEGIN (*implementation of Insert*)
END Insert;
PROCEDURE (t: Text) Delete (from, to: LONGINT);
BEGIN (*implementation of Delete*)
END Delete;
PROCEDURE (t: Text) Length (): LONGINT;
BEGIN (*implementation of Length*)
END Insert;
END Text1;
MODULE Text2; (*another implementation of the Text protocol*)
IMPORT TextProtocol;
TYPE Text = POINTER TO TextDesc;
TextDesc = RECORD (TextProtocol) END ; (*this means: “implements TextProtocol"*)
PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: LONGINT);
BEGIN (*implementation of Insert*)
END Insert;
PROCEDURE (t: Text) Delete (from, to: LONGINT);
BEGIN (*implementation of Delete*)
END Delete;
PROCEDURE (t: Text) Length (): LONGINT;
BEGIN (*implementation of Length*)
END Insert;
END Text2;
[*] https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#
[**] https://github.com/io-core/technotes/blob/main/technote014.md
More information about the Oberon
mailing list