comp.lang.ada
 help / color / mirror / Atom feed
* constant record components in Ada
@ 1997-06-27  0:00 Alexander V. Konstantinou
  1997-06-27  0:00 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alexander V. Konstantinou @ 1997-06-27  0:00 UTC (permalink / raw)



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.

Any ideas on how to tackle this ?

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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: constant record components in Ada
  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
@ 1997-06-27  0:00 ` Samuel Tardieu
  2 siblings, 0 replies; 5+ messages in thread
From: Samuel Tardieu @ 1997-06-27  0:00 UTC (permalink / raw)
  To: Alexander V. Konstantinou


>>>>> "Alexander" == Alexander V Konstantinou
>>>>> <akonstan@sutton.cs.columbia.edu> writes:

Alexander> Any ideas on how to tackle this ?

You can always make it a private record and define access functions
for both members and a modification function for the mutable member.

  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: constant record components in Ada
  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
  1997-06-27  0:00 ` Samuel Tardieu
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 1997-06-27  0:00 UTC (permalink / raw)



Alexander V. Konstantinou wrote:
> 
> 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.  

Why is a discrete type too restrictive? Discrete types include integer
types and enumeration types; what other types are appropriate for keys?
Maybe fixed length strings?

>On the other hand, making the
> discriminant a pointer is in my view an ugly solution since it keeps that
> extra pointer around.
> 
> Any ideas on how to tackle this ?

You could define a limited type to hold the key; then users could not
change it.

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

-- 
- Stephe




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: constant record components in Ada
  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
  1997-06-29  0:00   ` Alexander V. Konstantinou
  1997-06-27  0:00 ` Samuel Tardieu
  2 siblings, 1 reply; 5+ messages in thread
From: Robert Dewar @ 1997-06-27  0:00 UTC (permalink / raw)



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.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: constant record components in Ada
  1997-06-27  0:00 ` Robert Dewar
@ 1997-06-29  0:00   ` Alexander V. Konstantinou
  0 siblings, 0 replies; 5+ messages in thread
From: Alexander V. Konstantinou @ 1997-06-29  0:00 UTC (permalink / raw)



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




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1997-06-29  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
1997-06-29  0:00   ` Alexander V. Konstantinou
1997-06-27  0:00 ` Samuel Tardieu

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