comp.lang.ada
 help / color / mirror / Atom feed
From: Alexander V. Konstantinou <akonstan@sutton.cs.columbia.edu>
Subject: Re: constant record components in Ada
Date: 1997/06/29
Date: 1997-06-29T00:00:00+00:00	[thread overview]
Message-ID: <5p4qfr$rac@sutton.cs.columbia.edu> (raw)
In-Reply-To: dewar.867447101@merv


Robert Dewar <dewar@merv.cs.nyu.edu> wrote:
: 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 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.

How does that help me get the functionality of the C++ statement

	map_pair a(1, true); // ok

	a.first = 10;     // assignment of a read only member
			  // compile time error
	a.second = false; // ok

The Ada package using a combination of private/limited types would either
not provide any assignmet to a.first (what is the use then), or catch
a second assignment through an exception.  Now, I don't see how that is
equivalent.

: If the specification of your type is precisely that any arbitrary change
: to Value_Type is OK, but no change 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.

This is not an issue of syntactic sugar.  It is perfectly reasonable to
have a data structure in which you would like to provide write-once only
access to certain records (like the key used to create the map). In
addition, I wish to catch such errors at compile-time rather than run-
time.  I would have to go to great lengths, that is, create a new type
and support a limited view of its operations to achieve what C++ does
naturally (this is not a shot in the language war -- I can give many
examples in which Ada does things much more cleanly than C++).  To be
fair, I do not see an easy way to incorporate such fuctionality to Ada
since Ada takes a different view of constants than C++.

Alexander
-- 
Alexander V. Konstantinou              http://www.cs.columbia.edu/~akonstan/
akonstan@cs.columbia.edu               akonstan@acm.org




      reply	other threads:[~1997-06-29  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 ` Samuel Tardieu
1997-06-27  0:00 ` Stephen Leake
1997-06-27  0:00 ` Robert Dewar
1997-06-29  0:00   ` Alexander V. Konstantinou [this message]
replies disabled

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