comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: constant record components in Ada
Date: 1997/06/27
Date: 1997-06-27T00:00:00+00:00	[thread overview]
Message-ID: <dewar.867447101@merv> (raw)
In-Reply-To: 5p100i$pvn@sutton.cs.columbia.edu


A K asks

<<I am trying to obtain in Ada (95) the same functionality of the C++ struct
declaration :

        struct map_pair {
                const Key_Type first;
                Value_Type second;
        }

The idea is to provide a record in which the key value is constant, and the
value type is mutable.  The Ada books I have looked at suggest using
discriminants.  Unfortunately, discriminants have to be either discrete or
access types.  In my case Key_Type is a generic parameter and restricting it
to be a discrete type is too restrictive.  On the other hand, making the
discriminant a pointer is in my view an ugly solution since it keeps that
extra pointer around.
>>

The cleanest solution is to use a private extension so that Key_Type
is a hidden field. If you want users to have read access to it, you
can provide an appropriate access function.

You can of course make the entire type private, and provide exactly the
access you want in functional form as another alternative.

If the specification of your type is precisely that any arbitrary change
to Value_Type is OK, but no chanbge at all is valid to Key_Type, then
it is true that the C++ notation is more convenient, but this is a very
special case of the general issue of maintaining consistent data. 
Abstracting the data type is a much more general and usually preferable
solution.

We discussed during the Ada 9X design putting in a gizmo of this kind,
and decided it was not worth the added complexity of this feature to
provide a special syntax solution to a very limited case of the general
problem of controlling consistent access to exposed data.





  parent reply	other threads:[~1997-06-27  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-27  0:00 constant record components in Ada Alexander V. Konstantinou
1997-06-27  0:00 ` Stephen Leake
1997-06-27  0:00 ` Robert Dewar [this message]
1997-06-29  0:00   ` Alexander V. Konstantinou
1997-06-27  0:00 ` Samuel Tardieu
replies disabled

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