Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Operator Binding Example For Approach 2 A-Type

DEFINITION MODULE Dictionary;

(* Example library for a collection using operator bindings *)

TYPE Dictionary = OPAQUE ("A-Type");


(* key and value type *)

TYPE KeyType IS CARDINAL; (* arbitrary type *)

TYPE ValueType = ARRAY 255 OF CHAR; (* arbitrary type *)


(* null key *)

CONST [NIL] nullKey = 0; 


(* allocator *)

PROCEDURE [NEW] new ( VAR dict : Dictionary );
(* Allocates a new dictionary and passes a reference to it back in dict *)


(* accessor *)

PROCEDURE [.] valueForKey ( dict : Dictionary; key : KeyType; VAR found : BOOLEAN ) : ValueType;
(* Searches for key in dict, if found passes TRUE in found and returns its value, otherwise passes FALSE *)


(* mutators *)

PROCEDURE [!] storeValue ( dict : Dictionary; key : KeyType; value : ValueType );
(* Stores value for key in dict, overwrites the previous value if key is already present in dict *)

PROCEDURE [-] removeValue ( dict : Dictionary; key : KeyType );
(* Removes the entry whose key is key from dict *)


(* membership test *)

PROCEDURE [IN] keyExists ( dict : Dictionary; key : KeyType ) : BOOLEAN;
(* Returns TRUE if key is present in dict, otherwise returns FALSE *)


(* counter *)

PROCEDURE [COUNT] entryCount ( dict : Dictionary ) : LONGCARD;
(* Returns the number of entries stored in dict *)


(* iterator *)

PROCEDURE [FOR] nextEntry ( dict : Dictionary; VAR key : KeyType; VAR value : ValueType );
(* Finds the successor key of key and passes its key/value pair back in key and value.
   If the null key is passed in then the first key/value pair is passed back in key and value.
   If no successor key is found then the null key is passed back in key and value is undefined. *)


(* destructor *)

PROCEDURE [DISPOSE] dispose ( VAR dict : Dictionary );
(* Deallocates dict and passes NIL back in dict *)

END Dictionary.