From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6bf9d4ba0cfd8cb6 X-Google-Attributes: gid103376,public From: Brian Rogoff Subject: Re: Announce: OpenToken 2.0 released Date: 2000/02/01 Message-ID: #1/1 X-Deja-AN: 580491124 References: <3890C62B.18309585@telepath.com> <876unj$jcs$1@nnrp1.deja.com> <87799k$aai1@news.cis.okstate.edu> Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: nntp1.ba.best.com 949437127 226 bpr@206.184.139.136 MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-02-01T00:00:00+00:00 List-Id: On 1 Feb 2000, David Starner wrote: > On 01 Feb 2000 13:16:14 -0500, Hyman Rosen wrote: > >By the way, the normal C/C++ style for handling EOF is to have the > >return type of the character reader be such that it can hold any > >value of the character set, plus an out-of-band value representing > >EOF. The usual is '#define EOF -1' and 'int getchar()'. > > Which means that c: Character; c := getchar(); is illegal (at least > in Ada; in C it would get silently truncated.) It's a major pain > in C, and a well known source of bugs. How about making it like > procedure GetChar (EOF: in out boolean; Char: in out character);? There are a few other approaches I can think of to this issue (1) Exceptions: raise a Not_Found when input is exhausted. Some people hate this because "Exceptions are only for error handling, not control flow!". OCaml (and SML too I think) use exceptions for this, and Ada sometimes does (try reading a file stream without using File_Type...) (2) Provide a query on the sequence, like in Java, so you have code like while Has_More_Elements(Seq) loop Char := Get_Next_Element(Seq); ... end loop; I find this very readable. (3) Provide an option type like in (OCa|S)ML which wraps returned elements and forces the reader to unwrap them, like this type Option_T is (Some, None); generic type Element_T is private; package Options is type Optional_T(Option : Option_T) is record case Option is when Some => Data : Element_T; when None => null; end case; end record; end Options; loop Elem := Get_Next_Element(Seq); case Elem.Option is when Some => ... when None => exit; end case; end loop; This is too inefficient for reading chars and is very verbose in Ada; much less so in ML. -- Brian