comp.lang.ada
 help / color / mirror / Atom feed
* "NULL" returned by a function
@ 2014-08-08 21:10 Victor Porton
  2014-08-08 21:19 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Victor Porton @ 2014-08-08 21:10 UTC (permalink / raw)


What if I want to make a function which returns either a value of type T or 
a value signifying "nothing" (like null pointer)?

To make things worse, T is a controlled type, so if we return just "access 
T" the finalization of the object of type T may be easily overlooked.

What is the best way to implement this? variant record? whatever?

Or maybe should I modify the type T to allow it contain "null" state? (In 
fact the particular T which I use can be easily modified to allow storing 
there null and checking whether it's state is null. But I think on the way 
not to be dependent on the ability to change the definition of T.)

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  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-09 12:45   ` Victor Porton
  2014-08-08 21:25 ` Shark8
  2014-08-09 16:15 ` Maciej Sobczak
  2 siblings, 2 replies; 8+ messages in thread
From: Jeffrey Carter @ 2014-08-08 21:19 UTC (permalink / raw)


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;

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail
17

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  2014-08-08 21:10 "NULL" returned by a function Victor Porton
  2014-08-08 21:19 ` Jeffrey Carter
@ 2014-08-08 21:25 ` Shark8
  2014-08-09 16:15 ` Maciej Sobczak
  2 siblings, 0 replies; 8+ messages in thread
From: Shark8 @ 2014-08-08 21:25 UTC (permalink / raw)


On 08-Aug-14 15:10, 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)?

What's wrong with discriminated records?

Type Some_Container( Empty : Boolean ) is
   case Empty is
    When True  => Null;
    When False => Item : T;
   end case;
end record;

> To make things worse, T is a controlled type, so if we return just "access
> T" the finalization of the object of type T may be easily overlooked.

It seems that you're trying to program C/C++ in Ada, don't, that way 
lies only pain.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  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
  1 sibling, 1 reply; 8+ messages in thread
From: Victor Porton @ 2014-08-08 22:04 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  2014-08-08 22:04   ` Victor Porton
@ 2014-08-08 22:50     ` Jeffrey Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2014-08-08 22:50 UTC (permalink / raw)


On 08/08/2014 03:04 PM, Victor Porton wrote:
>
> But should I use a variant record EVEN IN THE CASE IF my data type T allows
> to store null values in it?

Probably not. But if you're writing Ada, and not pointer-heavy-language-in-Ada, 
then you're unlikely to be using access types.

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail
17


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  2014-08-08 21:19 ` Jeffrey Carter
  2014-08-08 22:04   ` Victor Porton
@ 2014-08-09 12:45   ` Victor Porton
  2014-08-09 15:13     ` Jeffrey Carter
  1 sibling, 1 reply; 8+ messages in thread
From: Victor Porton @ 2014-08-09 12:45 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  2014-08-09 12:45   ` Victor Porton
@ 2014-08-09 15:13     ` Jeffrey Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2014-08-09 15:13 UTC (permalink / raw)


On 08/09/2014 05:45 AM, Victor Porton wrote:
>
>     type String_Or_Null (Has_Value : Boolean := False) is
>        record
>           case Has_Value is
>              when False => null;
>              when True => Str: String;

    when True =>
       Value : Unbounded_String;

>           end case;
>        end record;

This assumes you need to distinguish between no string and the null string (""). 
Otherwise you can just return String.

-- 
Jeff Carter
"Insufficient laughter--that's grounds for divorce."
Play It Again, Sam
126

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: "NULL" returned by a function
  2014-08-08 21:10 "NULL" returned by a function Victor Porton
  2014-08-08 21:19 ` Jeffrey Carter
  2014-08-08 21:25 ` Shark8
@ 2014-08-09 16:15 ` Maciej Sobczak
  2 siblings, 0 replies; 8+ messages in thread
From: Maciej Sobczak @ 2014-08-09 16:15 UTC (permalink / raw)


On Friday, August 8, 2014 11:10:28 PM UTC+2, 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)?

The Ada.Containers.Indefinite_Holders package can be used to create a return type for such a function.

-- 
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-08-09 16:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2014-08-09 15:13     ` Jeffrey Carter
2014-08-08 21:25 ` Shark8
2014-08-09 16:15 ` Maciej Sobczak

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