From: pattis@beaver.cs.washington.edu (Richard Pattis)
Subject: Help with Exceptions for Generic Instantiation
Date: 13 Dec 91 17:27:22 GMT [thread overview]
Message-ID: <1991Dec13.172722.8432@beaver.cs.washington.edu> (raw)
I am trying to write various packages to access/advance-through a sequence
of characters (in a file, from a terminal, in an array, etc). I will then be
writing genric packages that are instantiated using this information; for
example, a lexical analyzer (or regular expression pattern matcher) taking
input from a file, a terminal, an array).
So far, one example is the following package specification
PACKAGE File_Stream IS
Open_Error : EXCEPTION;
Reset_Error : EXCEPTION;
No_More_Data_Error : EXCEPTION;
Close_Error : EXCEPTION;
TYPE Stream IS LIMITED PRIVATE;
PROCEDURE Open (S : IN OUT Stream; Info : IN STRING);
PROCEDURE Reset (S : IN OUT Stream);
PROCEDURE Get (C : OUT CHARACTER; S : IN OUT Stream);
PROCEDURE Close (S : IN OUT Stream);
PRIVATE
TYPE Stream IS NEW Text_IO.File_Type;
END File_Stream;
A simple echo utility might then be written as follows:
WITH Terminal_Utility, String_Utility; -- My own I/O interfaces
GENERIC
Message : IN STRING;
TYPE Stream IS LIMITED PRIVATE;
WITH PROCEDURE Open (S : IN OUT Stream; Info : IN STRING);
WITH PROCEDURE Reset (S : IN OUT Stream);
WITH PROCEDURE Get (C : OUT CHARACTER; S : IN OUT Stream);
WITH PROCEDURE Close (S : IN OUT Stream);
PROCEDURE Echo;
PROCEDURE Echo IS
PACKAGE SU RENAMES String_Utility;
PACKAGE TTY RENAMES Terminal_Utility;
S : Stream;
C : CHARACTER;
BEGIN
Open(S, TTY.Prompt(Message));
BEGIN
LOOP
Get(C, S);
TTY.Announce(C);
END LOOP;
EXCEPTION
WHEN OTHERS => Close(S); -- No_More_Data_Error
END;
TTY.Announce(SU.CR);
END Echo;
The problem is that real programs/packages (beyond the complexity of Echo)
will need to individually handle various exceptions by name, yet I cannot find
any way to instantiate generics using exceptions. Am I overlooking something
obvious or is there something subtle that will help solve my problem?
I realize that I could supply generic predicates to "replace" the exceptions,
but my question still stands - is there a reasonable way to use exceptions.
------------------------------------------------------------------------------
Richard E. Pattis "Programming languages are like
Department of Computer Science pizzas - they come in only "too"
and Engineering sizes: too big and too small."
University of Washington
Seattle, WA 98195 "When debugging, a novice programmer
pattis@cs.washington.edu inserts corrective code; an
voice: (206) 685-1218 experienced programmer removes
fax : (206) 543-2969 defective code."
--
------------------------------------------------------------------------------
Richard E. Pattis "Programming languages are like
Department of Computer Science pizzas - they come in only "too"
and Engineering sizes: too big and too small."
next reply other threads:[~1991-12-13 17:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-12-13 17:27 Richard Pattis [this message]
-- strict thread matches above, loose matches on Subject: below --
1991-12-13 20:10 Help with Exceptions for Generic Instantiation Arthur Evans
1991-12-16 14:28 Brad Balfour
1991-12-16 21:17 Brad Balfour
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox