comp.lang.ada
 help / color / mirror / Atom feed
From: Victor Porton <porton@narod.ru>
Subject: Re: "NULL" returned by a function
Date: Sat, 09 Aug 2014 15:45:46 +0300
Date: 2014-08-09T15:45:46+03:00	[thread overview]
Message-ID: <ls555o$ctl$1@speranza.aioe.org> (raw)
In-Reply-To: ls3esh$e4r$1@dont-email.me

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;

   type String_Or_Null (Has_Value : Boolean := False) is
      record
         case Has_Value is
            when False => null;
            when True => Str: String;
         end case;
      end record;

rdf-auxilary.ads:22:31: unconstrained subtype in component declaration

What it the RIGHT (not just some) way of dealing with this case?

I can add to String_Or_Null additional discriminant "Length: Natural", but 
it is a superfluous information when Has_Value is False.

Finally I can make a private type (to hide the superfluous discriminant) 
from this. Good idea?

-- 
Victor Porton - http://portonvictor.org

  parent reply	other threads:[~2014-08-09 12:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-08 21:10 "NULL" returned by a function Victor Porton
2014-08-08 21:19 ` Jeffrey Carter
2014-08-08 22:04   ` Victor Porton
2014-08-08 22:50     ` Jeffrey Carter
2014-08-09 12:45   ` Victor Porton [this message]
2014-08-09 15:13     ` Jeffrey Carter
2014-08-08 21:25 ` Shark8
2014-08-09 16:15 ` Maciej Sobczak
replies disabled

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