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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: Victor Porton Newsgroups: comp.lang.ada Subject: Re: "NULL" returned by a function Date: Sat, 09 Aug 2014 01:04:50 +0300 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: AnnUDmZwVERVUXyHDyOl5A.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.12.4 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:21588 Date: 2014-08-09T01:04:50+03:00 List-Id: Jeffrey Carter wrote: > On 08/08/2014 02:10 PM, Victor Porton wrote: >> What if I want to make a function which returns either a value of type T >> or a value signifying "nothing" (like null pointer)? > > Typically one should use a variant record with one variant for "nothing" > and another for the value: > > type Result (Has_Value : Boolean := False) is record > case Has_Value is > when False => > null; > when True => > Value : T; > end case; > end record; But should I use a variant record EVEN IN THE CASE IF my data type T allows to store null values in it? The benefit of using a variant record in this case that it makes the intention of the programmer more clear and thus increases readability and reliability. (It makes less like for a programmer to forget that a T value may hold a null access somewhere and this needs to be taken into account.) The drawbacks are: longer source code and probably lower performance. -- Victor Porton - http://portonvictor.org