comp.lang.ada
 help / color / mirror / Atom feed
* Help with Exceptions for Generic Instantiation
@ 1991-12-13 17:27 Richard Pattis
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Pattis @ 1991-12-13 17:27 UTC (permalink / 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."

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help with Exceptions for Generic Instantiation
@ 1991-12-13 20:10 Arthur Evans
  0 siblings, 0 replies; 4+ messages in thread
From: Arthur Evans @ 1991-12-13 20:10 UTC (permalink / raw)


pattis@cs.washington.edu (Richard Pattis) asks about parameterizing a
generic with an exception.

Well, if you are missing something it's because it isn't there.  Ada 83
has no mechanism to do what you want.  Ada 9X will, but that doesn't
help much right now.

Art Evans
----------------------------------------------
Arthur Evans, Jr, PhD		Ada Consultant
461 Fairview Road
Pittsburgh PA  15238-1933
412-963-0839
ae@sei.cmu.edu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help with Exceptions for Generic Instantiation
@ 1991-12-16 14:28 Brad Balfour
  0 siblings, 0 replies; 4+ messages in thread
From: Brad Balfour @ 1991-12-16 14:28 UTC (permalink / raw)


ae@sei.cmu.edu (Arthur Evans) replies:

>pattis@cs.washington.edu (Richard Pattis) asks about parameterizing a
>generic with an exception.

>Well, if you are missing something it's because it isn't there.  Ada 83
>has no mechanism to do what you want.  Ada 9X will, but that doesn't
>help much right now.

>Art Evans

Well... Not necessarily.
Draft 3.1 of the Ada 9X Mapping Specification from August 1991 does not
have exceptions as generic formal parameters. And, (IMHO) it doesn't look likel
y
that this will change unless people demand it.

However, the Mapping/Revision Team has proposed another mechanism that might
work in this case: Generic formal package parameters.

I have attempted to work out Rich's example with the new (as of v3.1) syntax.

    -- new --
  Generic  -- must be generic to be passed as a generic formal parameter
    -- rest is same as before --
  PACKAGE File_Stream IS
    ...
  END File_Stream;

A simple echo utility might then be written as follows (modified for 9X v3.1):

  WITH Terminal_Utility, String_Utility;        -- His own I/O interfaces
  GENERIC
    Message : IN STRING;
    with package A_Stream is new File_Stream(<>) ;
        -- any instantiation of File_Stream will match this parameter
  PROCEDURE Echo;


  PROCEDURE Echo IS
    PACKAGE SU  RENAMES  String_Utility;
    PACKAGE TTY RENAMES Terminal_Utility;

    S : A_Stream.Stream;
    C : CHARACTER;

  BEGIN
    Open(S, TTY.Prompt(Message));
    BEGIN
      LOOP
        A_Stream.Get(C, S);
        TTY.Announce(C);
      END LOOP;
    EXCEPTION
      WHEN A_Stream.No_More_Data_Error =>  -- now the exception name is visible
            Close(S);
    END;
    TTY.Announce(SU.CR);
  END Echo;

Hope this helps.

------
Brad Balfour
EVB Software Engineering, Inc.
1-800-877-1815
brad@terminus.umd.edu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help with Exceptions for Generic Instantiation
@ 1991-12-16 21:17 Brad Balfour
  0 siblings, 0 replies; 4+ messages in thread
From: Brad Balfour @ 1991-12-16 21:17 UTC (permalink / raw)


whoops. I forgot the package prefix in two places in the body of Echo.
The code should read:

  BEGIN
    A_Stream.Open(S, TTY.Prompt(Message));
    BEGIN
      LOOP
        A_Stream.Get(C, S);
        TTY.Announce(C);
      END LOOP;
    EXCEPTION
      WHEN A_Stream.No_More_Data_Error =>  -- now the exception name is visible
        A_Stream.Close(S);
    END;
    TTY.Announce(SU.CR);
  END Echo;

----
Brad Balfour
EVB Software Engineering, Inc.
brad@terminus.umd.edu

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1991-12-16 21:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-12-13 20:10 Help with Exceptions for Generic Instantiation Arthur Evans
  -- strict thread matches above, loose matches on Subject: below --
1991-12-16 21:17 Brad Balfour
1991-12-16 14:28 Brad Balfour
1991-12-13 17:27 Richard Pattis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox