Project

General

Profile

Some new concept for modules options

Added by Ivan Denisov about 3 years ago

Great project!
I looked the module Lists and see some new concept of Module argument.
Can you give some example? Or maybe there are already some there, which I did not find?


Replies (2)

RE: Some new concept for modules options - Added by Florian Negele about 3 years ago

Thank you very much. In case you are referring to generic modules, consider a basic stack module that stores values of type Element in an array of length Length:

MODULE Stack;

TYPE Element = INTEGER;
CONST Length = 100;

VAR elements: ARRAY Length OF Element; size: LENGTH;

PROCEDURE Push* (element-: Element);
BEGIN elements[size] := element; INC (size);
END Push;

PROCEDURE Pop* (VAR element: Element);
BEGIN DEC (size); element := elements[size];
END Pop;

BEGIN
    size := 0;
END Stack.

Instead of replicating the complete module by hand if another type or length is required, generic modules allow expressing operations like Push and Pop that do not depend on the actual element type or length using module parameters. Within a generic module, these parameters still act as constants or type definitions as before:

MODULE Stack (Element, Length);

VAR elements: ARRAY Length OF Element; size: LENGTH;

PROCEDURE Push* (element-: Element);
BEGIN elements[size] := element; INC (size);
END Push;

PROCEDURE Pop* (VAR element: Element);
BEGIN DEC (size); element := elements[size];
END Pop;

BEGIN
    size := 0;
END Stack.

The actual values for both parameters are passed by the user of the generic module when importing it:

MODULE Test;

IMPORT Stack (INTEGER, 100);

BEGIN
    Stack.Push (0);
END Test.

RE: Some new concept for modules options - Added by Ivan Denisov about 3 years ago

Thanks for explanation, clear example! I got it.

    (1-2/2)