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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4180a73b05d119c7,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-03-17 13:10:52 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news.teledanmark.no!news01.chello.no!nntp.newmedia.no!uio.no!newsfeed1.uni2.dk!news.get2net.dk.POSTED!53ab2750!not-for-mail Sender: malo@valhal.vikingnet Newsgroups: comp.lang.ada Subject: ML-like alternatives to out parameters for functions From: Mark Lorenzen Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: 17 Mar 2003 23:17:12 +0100 NNTP-Posting-Host: 62.84.221.216 X-Complaints-To: abuse@colt-telecom.dk X-Trace: news.get2net.dk 1047935451 62.84.221.216 (Mon, 17 Mar 2003 22:10:51 CET) NNTP-Posting-Date: Mon, 17 Mar 2003 22:10:51 CET Organization: Colt Telecom Kunde X-Received-Date: Mon, 17 Mar 2003 22:10:52 MET (news01.chello.no) Xref: archiver1.google.com comp.lang.ada:35422 Date: 2003-03-17T23:17:12+01:00 List-Id: There has lately been some discussions about Ada's missing capabilities for specifying out parameters for functions. Apparently this is a needed feature and an AI (AI-323) has been issued. An out parameter can be used when the validity of the result of evaluating a function is not always well-defined. For example: function Calculate (Argument : in AT; Result_Valid : out Boolean) return RT; Use of the function could be as: Result := Calculate (Arg, Valid); if Valid then .. do stuff with Result .. else .. handle error .. end if; It seems to me that using a discriminated record type as return type in order to mimic ML's Option datatype could solve such a (frequently) occurring problem. But of course I am not the first to come up with this suggestion, so what is wrong with it? Is it too slow? The package spec is shown below and should be self-explanatory. Regards, - Mark Lorenzen generic type Value_Type is private; package Optional_Value is type Optional_Value_Type is private; Undefined_Value : exception; function Some (Value : in Value_Type) return Optional_Value_Type; function None return Optional_Value_Type; -- May raise Undefined_Value; function Value_Of (Element : in Optional_Value_Type) return Value_Type; function Is_Defined (Element : in Optional_Value_Type) return Boolean; private type Optional_Value_Type (Defined : Boolean := False) is record case Defined is when True => Value : Value_Type; when False => null; end case; end record; end Optional_Value;