comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: handling null pointers/records to functions
Date: Sat, 21 Aug 2004 19:54:23 GMT
Date: 2004-08-21T19:54:23+00:00	[thread overview]
Message-ID: <PLNVc.8968$3O3.2832@newsread2.news.pas.earthlink.net> (raw)
In-Reply-To: <41274733@dnews.tpgi.com.au>

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




  parent reply	other threads:[~2004-08-21 19:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-21 12:59 handling null pointers/records to functions quasar
2004-08-21 13:55 ` Jim Rogers
2004-08-21 17:11   ` Georg Bauhaus
2004-08-21 19:54 ` Jeffrey Carter [this message]
2004-08-22  4:12 ` Steve
replies disabled

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