comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: if Restrict_Func /= null confusion
Date: Sun, 30 Mar 2008 17:37:26 -0400
Date: 2008-03-30T17:37:26-04:00	[thread overview]
Message-ID: <wccabkfuc4p.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 95f36a62-b94c-4211-896a-300fa98932b7@x41g2000hsb.googlegroups.com

ma740988@gmail.com writes:

> Well folks its been at least 4 years since I've perused and/or written
> Ada source.  That aside I'm perusing source code written in Ada.   So
> consider:
>
> -- used to restrict a variable within a range
> -- for example: -PI to +PI
> type Restrict_Function is access function (X: Real4) return Real4;
>
> type Filt_Data is
>   record
>      Restrict_Func   : Restrict_Function ;
>
>   end record ;
>
>
>    --  within a procedure we have
>   Fdata             : Filt_Data;
>   Restrict_Func : Restrict_Function := FData.Restrict_Func ;
>
>   if  Restrict_Func /= null then
>    -- stuff
>   endif
>
> At issue:  I'm not following the conditional logic 'if (Restrict_Func /
> = null)'.  Not understanding the impetus behind the check for null.

Restrict_Func is a pointer to a function.  To call the function,
you say something like "Restrict_Func.all(X)".  You can leave
the ".all" implicit: "Restrict_Func(X)", but the ".all" still
happens.  ".all" on a null pointer raises an exception.

In your example, presumably "stuff" calls the function -- so it needs to
be protected by the 'if'.

It's usually better to declare:

    type Restrict_Function is not null access function ...;
                              ^^^^^^^^

so you don't need to worry about null, which is just a tripping hazard.

Or use primitive operations of tagged types and dispatching calls.

- Bob



  parent reply	other threads:[~2008-03-30 21:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-30 20:58 if Restrict_Func /= null confusion ma740988
2008-03-30 21:34 ` jimmaureenrogers
2008-03-30 21:36 ` Ludovic Brenta
2008-03-30 21:37 ` Robert A Duff [this message]
2008-03-30 21:44 ` Georg Bauhaus
replies disabled

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