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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,19c9b8adc049bc94 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 MSIE X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: handling null pointers/records to functions References: <41274733@dnews.tpgi.com.au> In-Reply-To: <41274733@dnews.tpgi.com.au> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Sat, 21 Aug 2004 19:54:23 GMT NNTP-Posting-Host: 63.184.104.121 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.pas.earthlink.net 1093118063 63.184.104.121 (Sat, 21 Aug 2004 12:54:23 PDT) NNTP-Posting-Date: Sat, 21 Aug 2004 12:54:23 PDT Xref: g2news1.google.com comp.lang.ada:2915 Date: 2004-08-21T19:54:23+00:00 List-Id: quasar wrote: > Hi , I was wondering what is the best way to handle null records/pointers > passed to functions with a return type that is generic? > > for example in this snippet of code node_ptr_type is a record holding a > single data value of type Data_Type > > function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is > begin > if node_ptr /= null then > return Node_Ptr.Data; > else > Put_Line("Node passed to FUNCTION DATA_PART is null"); > {(**)at this point i do not know what to do... do I terminate the > program, do I return some form of null object} > end if; > end Data_Part; > > What is the most recommended way to handle this? Point (**) has got me > stuck. It appears you are dealing with a homework assignment for a generic linked data structure, and Data_Type is a generic formal parameter. Probably the requirements for the data structure indicate that it is invalid to attempt to obtain the data from a non-existent node. A null value for the parameter would be a non-existent node, so calling this function with null is a violation of this precondition, and raising an exception is appropriate. Alternatively, you could use SPARK to prove that the precondition is never violated, and the body of the function could simply be return Node_Ptr.Data; -- Jeff Carter "Son of a window-dresser." Monty Python & the Holy Grail 12