Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Modula-3
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Generics=== A generic interface and its corresponding generic module, prefix the <code>INTERFACE</code> or <code>MODULE</code> keyword with <code>GENERIC</code>, and take as formal arguments other interfaces. Thus (like [[Template (C++)|C++ templates]]) one can easily define and use abstract data types, but unlike [[C++]], the granularity is at the module level. An interface is passed to the generic interface and implementation modules as arguments, and the compiler will generate concrete modules. For example, one could define a GenericStack, then instantiate it with interfaces such as <code>IntegerElem</code>, or <code>RealElem</code>, or even interfaces to Objects, as long as each of those interfaces defines the properties needed by the generic modules. The bare types <code>INTEGER</code>, or <code>REAL</code> can't be used, because they are not modules, and the system of generics is based on using modules as arguments. By comparison, in a C++ template, a bare type would be used. '''FILE: IntegerElem.i3''' <syntaxhighlight lang="modula2" highlight="1"> INTERFACE IntegerElem; CONST Name = "Integer"; TYPE T = INTEGER; PROCEDURE Format(x: T): TEXT; PROCEDURE Scan(txt: TEXT; VAR x: T): BOOLEAN; END IntegerElem. </syntaxhighlight> '''FILE: GenericStack.ig''' <syntaxhighlight lang="modula2" highlight="1"> GENERIC INTERFACE GenericStack(Element); (* Here Element.T is the type to be stored in the generic stack. *) TYPE T = Public OBJECT; Public = OBJECT METHODS init(): TStack; format(): TEXT; isEmpty(): BOOLEAN; count(): INTEGER; push(elm: Element.T); pop(VAR elem: Element.T): BOOLEAN; END; END GenericStack. </syntaxhighlight> '''FILE: GenericStack.mg''' <syntaxhighlight lang="modula2" highlight="1"> GENERIC MODULE GenericStack(Element); < ... generic implementation details... > PROCEDURE Format(self: T): TEXT = VAR str: TEXT; BEGIN str := Element.Name & "Stack{"; FOR k := 0 TO self.n -1 DO IF k > 0 THEN str := str & ", "; END; str := str & Element.Format(self.arr[k]); END; str := str & "};"; RETURN str; END Format; < ... more generic implementation details... > END GenericStack. </syntaxhighlight> '''FILE: IntegerStack.i3''' <syntaxhighlight lang="modula2"> INTERFACE IntegerStack = GenericStack(IntegerElem) END IntegerStack. </syntaxhighlight> '''FILE: IntegerStack.m3''' <syntaxhighlight lang="modula2"> MODULE IntegerStack = GenericStack(IntegerElem) END IntegerStack. </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)