comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Type_Invariant and Finalize
Date: Fri, 18 Jul 2014 17:48:16 -0400
Date: 2014-07-18T17:48:16-04:00	[thread overview]
Message-ID: <wccbnsm1gpr.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: slrnlshh8m.i0l.lithiumcat@nat.rebma.instinctive.eu

Natasha Kerensikova <lithiumcat@instinctive.eu> writes:

> I have been toying with the idea of putting the null exclusion in the
> record component or array declaration:
>
>    type Array_Of_Indefinite_Elements is array (Index range <>)
>      of not null Element_Access;
>
>    type Node is record
>       Key : not null Key_Access;
>       Element : not null Element_Access;
>    end record;

I suggest using predicates instead of "not null":

    type Optional_Element_Access is access all Element'Class;
    subtype Element_Access is Optional_Element_Access with
        Predicate => Element_Access /= null;

"Predicate" is gnat-specific.  Use "Dynamic_Predicate" if you want to be
portable.  You can mostly use Element_Access, but use
Optional_Element_Access in the rare cases (e.g. inside Finalize)
where you want to allow null.

One problem with "not null" is that you can't create an object, and then
initialize it later.  Consider:

    X : Positive;
    ... -- No reads of X here.
    X := Something(...); -- Here we initialize X to some value >= 1.
    ... -- Now we can read X.

That works fine.  But similar code doesn't work with "not null":

    X : Element_Access;
    ... -- No reads of X here.
    X := Something(...); -- Here we initialize X to some non-null value.
    ... -- Now we can read X.

If Element_Access is declared "not null", then the declaration of X
blows up.  Using a predicate instead, it works the same way as the
Positive example.

- Bob


  reply	other threads:[~2014-07-18 21:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-17 18:15 Type_Invariant and Finalize Natasha Kerensikova
2014-07-17 20:49 ` Simon Wright
2014-07-18  6:56   ` Natasha Kerensikova
2014-07-18 21:48     ` Robert A Duff [this message]
2014-07-17 21:30 ` Type_Invariant and instance creation (was: Type_Invariant and Finalize) Simon Wright
2014-07-21 23:29   ` Randy Brukardt
2014-07-22  1:13     ` Type_Invariant and instance creation Shark8
replies disabled

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