gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gm2] Returning a string constant


From: Christian Maurer
Subject: Re: [Gm2] Returning a string constant
Date: Mon, 27 Oct 2008 15:04:22 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

On Mon, Oct 27, 2008 at 09:10:12AM -0400, Breeden, Thomas (tmb) wrote:
> Hi all,
> 
> Actually, in ISO Standard Modula-2, it is legal.
> 
> One of the changes made from Wirth's definition of the language is that 
> functions can
> return structured types.
> 
> I tried the test program out in my copy of the late, lamented, Stony Brook 
> compiler and
> it compiled and ran perfectly.
> 
> I'm pretty much in sympathy with all the arguments against, but I feel that 
> it would be
> better for M2 if GM2 did implement the ISO standard.
> 
> regards
> 
> Tom
> address@hidden
> 
> See:
> 
>    ISO/IEC 10514-1, the standard for Modula-2: changes, clarifications and 
> additions
>    Source       ACM SIGPLAN Notices archive
>    Volume 31 ,  Issue 8  (August 1996)
>    Pages: 84 - 95
>    Year of Publication: 1996
>    ISSN:0362-1340
>    Authors
>       M. Schönhacker     Vienna University of Technology, Austria
>       C. Pronk   Delft University of Technology, The Netherlands
>    Publisher
>       ACM  New York, NY, USA
> 
> 
> > -----Original Message-----
> > From: address@hidden [mailto:gm2-
> > address@hidden On Behalf Of Andreas Fischlin
> > Sent: October 26, 2008 10:26 AM
> > To: Scott Robinson
> > Cc: address@hidden
> > Subject: Re: [Gm2] Returning a string constant
> >
> > Dear Scott,
> >
> > In standard Modula-2 yes, this is illegal. Wirth (1983) writes on p.
> > 52: "Only one value, however, can be returned as the result of a
> > function. This value, moreover, cannot be of a structured type."

Dear Sirs,

everything right so far.
However, returning structured types as opaque typs seems to me
to be the better solution (more in the sense of the modern style
of object-based/-oriented programming).

Thus, my proposal:

---  8<  -------------------------------------------------------
DEFINITION MODULE AbstractStrings;

CONST M = 200;
TYPE Objects; (* Strings of lenghth < M.
                 The module also handles a queue of
                 such strings, that is initially empty. *)

PROCEDURE new (S: ARRAY OF CHAR): Objects;
(* pre:  -
   val:  Initialized String, that contains exactly the
         characters in S in their order in S with final 0C. *)

PROCEDURE write (S: Objects);
(* pre:  S is initialized.
   post: S is appended to the queue. *)

PROCEDURE out;
(* pre:  -
   post: All strings in the queue are - in their order in the
         queue - written to the screen, starting with a new line
         (if the screen was full, its contents are scrolled).
         The queue is now empty. *)

END AbstractStrings.
---  8<  -------------------------------------------------------
IMPLEMENTATION MODULE AbstractStrings;

FROM Storage IMPORT ALLOCATE;
FROM InOut IMPORT WriteString, WriteLn;
FROM Strings IMPORT Assign;

TYPE
  Indices = [0..M];
  Objects = POINTER TO ARRAY Indices OF CHAR;

PROCEDURE new (S: ARRAY OF CHAR): Objects;
VAR String: Objects;
BEGIN
  NEW (String);
  (* carelly check Strings.def: order *)
  Assign (String^, S);
  (* may differ   ^^^^^^^  ^   in different systems ! *)
  RETURN String
END new;

PROCEDURE write (String: Objects);
BEGIN
  WriteString (String^)
END write;

PROCEDURE out;
BEGIN
  WriteLn
END out;

END AbstractStrings.
---  8<  -------------------------------------------------------
MODULE test1;

FROM AbstractStrings IMPORT Objects, new, write, out;

VAR
  S: Objects;
BEGIN
  S:= new ("test");
  write (new ('Test results = "'));
  write (S);
  write (new ('"'));
  out
END test1.
---  8<  -------------------------------------------------------

Greetings,

Ch. Maurer
-- 
Dr. Christian Maurer            address@hidden
Leiter Lehrerweiterbildung          lwb.mi.fu-berlin.de
Institut für Informatik        Freie Universität Berlin
Takustr. 9, 14195 Berlin   838 75 216, priv. 214 78 530




reply via email to

[Prev in Thread] Current Thread [Next in Thread]