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 3 S-Type

DEFINITION MODULE String ("S-Type");

(* Example library for dynamic strings using operator bindings *)

TYPE String = OPAQUE;


(* allocator *)

PROCEDURE [NEW] new ( VAR s : String );
(* Allocates a new string and passes a reference to it back in s *)


(* literal assignment *)

PROCEDURE [:=] assign ( VAR s : String; literal : ARRAY OF CHAR );


(* concatenation *)

PROCEDURE [+] concatenate ( s1, s2 : String ) : String;


(* relational operations *)

PROCEDURE [=] isEqual ( s1, s2 : String ) : BOOLEAN;

PROCEDURE [>] isGreater ( s1, s2 : String ) : BOOLEAN;

PROCEDURE [<] isLess ( s1, s2 : String ) : BOOLEAN;


(* length *)

PROCEDURE [LENGTH] length ( s : String ) : LONGCARD;


(* destructor *)

PROCEDURE [DISPOSE] dispose ( VAR s : String );
(* Deallocates string s and passes NIL back in s *)


(* IO procedures *)

PROCEDURE Read ( f : File; VAR s : String );

PROCEDURE Write ( f : File; s : String );

PROCEDURE WriteF ( f : File; fmtStr : ARRAY OF CHAR;
                   items : CARDINAL; VARIADIC v[items] OF s : String );

END String.