comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Question on type invariants
Date: Wed, 25 Jan 2017 16:06:26 -0600
Date: 2017-01-25T16:06:26-06:00	[thread overview]
Message-ID: <o6b7h2$bdl$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: 65f79e53-a468-4b77-8ee1-440c26a09371@googlegroups.com

"reinkor" <reinkor@gmail.com> wrote in message 
news:65f79e53-a468-4b77-8ee1-440c26a09371@googlegroups.com...
...
> I can program the whole thing and keep the concepts in my head, but is it 
> a natural
>"Ada way" to secure that S is up-to-date when used? Is "type invariants" of 
>any help?

Perhaps, if you can only export up-to-date versions of S. Type invariants 
are (mostly) enforced at the package boundary, that is when objects are 
passed in and out of the package that implements S.

I generally find that type invariants are too limiting. It's usually easier 
to have a dynamic predicate on a subtype; you can then apply that subtype to 
each place where the property is required. Something like:

    type S is ...
    subtype Up_to_Date_S is S
       with Dynamic_Predicate => Is_Up_to_Date (Up_to_Date_S);

Then all of the operations that require up-to-date S values can have 
parameters of Up_to_Date_S; the compiler will insert a check that it is in 
fact up-to-date. (But be careful: if you use such subtypes in places where 
the object can become stale behind the scenes, Ada will not recheck the 
predicate - dynamic predicates are primarily checked when a subtype 
conversion occurs, and usually only when a subtype *change* happens.)

In either case, you have to be able to write a function which returns True 
or False as to whether an object of type S is up-to-date. (If you can't do 
that, you can't use any sort of contract for the obvious reason that a 
contract has to be able to tell if it is satisfied.)

                                   Randy.



  reply	other threads:[~2017-01-25 22:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-25  8:38 Question on type invariants reinkor
2017-01-25 22:06 ` Randy Brukardt [this message]
2017-01-26  4:31   ` reinkor
2017-01-26  4:32   ` reinkor
2017-01-26  8:38 ` Dmitry A. Kazakov
2017-01-27  9:20   ` reinkor
2017-01-27  9:47     ` Dmitry A. Kazakov
2017-01-31  6:44       ` reinkor
2017-01-26 16:18 ` Robert Eachus
2017-01-27  5:35   ` reinkor
2017-01-27  5:47   ` reinkor
replies disabled

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