From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Unknown constraints and type composition
Date: Thu, 14 Jun 2018 19:53:07 +0200
Date: 2018-06-14T19:53:07+02:00 [thread overview]
Message-ID: <pfua23$4jt$1@dont-email.me> (raw)
In-Reply-To: <pfu23n$csb$1@dont-email.me>
On 06/14/2018 05:37 PM, Alejandro R. Mosteo wrote:
> I think I have read somewhere that types with unknown constraints are a good way
> of ensuring you (or your users) don't end with uninitialized values:
>
> types Whatever (<>) is [limited] private;
>
> function Create return Whatever;
It's one way. There are others that might be better. Unknown discriminants are
more for generic formal types, to show that the generic accepts indefinite
actual types.
One way to deal with this is to make the full type a record with reasonable
defaults for all the components. This works for all versions of the language.
Another is to make the type a descendant of [Limited_}Controlled and override
Initialize. This works for Ada 95 and later.
Another way is to have
function Initialized (Thing : Whatever) return Boolean;
that returns True if its parameter has been initialized. Have a
Dynamic_Predicate on Whatever that Initialized returns True. Have a
postcondition on your New_Whatever function that its return value is
Initialized. Make the full type a record with an Initialized component default
initialized to False. This only works for Ada 12.
Another is to leave off the predicate, and instead give all operations on the
type the precondition that the value is Initialized. This only works for Ada 12,
but it can be emulated in any version of Ada with manual checks of the precondition.
> This seems nice at first sight but when these types have any likelihood of
> ending as members of another type you will hit the "unconstrained member" problem.
After having made an effort to make the type indefinite, you should not be
surprised that it's an indefinite type.
Part of design is to try to anticipate all reasonable uses for a type, and
choose an approach that works for them all. If this is a reasonable use of the
type, then unknown discriminants is not a suitable approach.
--
Jeff Carter
"When Roman engineers built a bridge, they had to stand under it
while the first legion marched across. If programmers today
worked under similar ground rules, they might well find
themselves getting much more interested in Ada!"
Robert Dewar
62
next prev parent reply other threads:[~2018-06-14 17:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-14 15:37 Unknown constraints and type composition Alejandro R. Mosteo
2018-06-14 16:19 ` Dmitry A. Kazakov
2018-06-14 16:58 ` sbelmont700
2018-06-14 17:53 ` Jeffrey R. Carter [this message]
2018-06-15 5:13 ` J-P. Rosen
2018-06-15 9:34 ` Alejandro R. Mosteo
2018-07-08 13:53 ` Jacob Sparre Andersen
2018-06-14 21:28 ` Randy Brukardt
2018-06-15 9:37 ` Alejandro R. Mosteo
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox