* Re: Two simple language questions
1998-01-07 0:00 Chip Richards
@ 1998-01-07 0:00 ` Matthew Heaney
1998-01-07 0:00 ` Robert Dewar
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Matthew Heaney @ 1998-01-07 0:00 UTC (permalink / raw)
In article <68uq34$7tk@tomquartz.niestu.com>, chipr@niestu.com (Chip
Richards) wrote:
>I hope.
>
>First: It says in the Intro to Ada 9X, in Chapter 2, "General access types can
>also be used to program static ragged arrays as for example a table of
>messages of different lengths." Now, I know this has been asked before, some
>time ago, but I cannot seem to locate the text of the exchange. What I want
>to know is, exactly what is the syntax for doing this? I've so far used
>allocators, i.e.:
>
> strs: StringArr :=
> (
> new string'("Line 1"),
> new string'("Line 2"),
> ...
> new string'("Line the Last")
> );
>
>Which works great, but isn't exactly the "static" ragged arrays mentioned in
>the intro. What I want is something like "Line 1"'access, but of course that
>doesn't work, nor have any of the sixteen-umpty variations that I've tried.
>The RM just said that in X'access, X must be an aliased view of an object.
>Well, hmph. Hep me, hep me, please.
I think an example is in John Barnes' book, but here's a solution:
declare
Line_1 : aliased constant String := "this is line 1";
Line_2 : aliased constant String := "this is the text of line 2";
...
Line_Last : aliased constant String := "last";
type String_Access_Constant is
access constant String;
type String_Array is
array (Positive range <>) of String_Access_Constant;
The_Lines : constant String_Array :=
(Line_1'Access, Line_2'Access, ..., Line_Last'Access);
begin
...
end;
>
>Second, a bit more of a philosophy question, I think. I've had a couple of
>occasions where I've created Ada95 child packages, and in those packages, I've
>wanted to extend enumeration types defined by the parent. As an example:
>
> type Colors is ( Red, Green, Blue );
>
>And in the child, I'd like to be able to say
>
> type More_Colors is new Colors with ( Yellow, Purple );
>
>Except, of course, that isn't Ada. So, what's considered the best approach to
>this sort of thing? Just re-list the constants of the parent type, and add
>the new constants into the list? Seems a bit repetetive and error-prone to
>me, and I was just wondering if I'd missed someone proposing a more elegant
>solution. Or am I really off the rails and missing something totally obvious?
No, you can't extend an enumeration type. You can't extend the range of
any scalar type, not just enumeration types.
You can get at the base range of the type by using T'Base. However, for
enumeration types, the first named subtype is the base type (by
definition), so T'Base wouldn't buy you anything anyway.
If you want to "extend" a scalar type, then just used constant objects an
integer type, not an enumeration type:
type Color_Base is new Natural;
subtype Color is Color_Base range 0 .. 2;
Red : constant Color := 0;
Green : constant Color := 1;
Blue : constant Color := 2;
subtype Extended_Color is Color_Base range 0 .. 4;
Yellow : constant Extended_Color := 3;
Purple : constant Extended_Color := 4;
Declare objects and parameters of type Color_Base when you want to accept
any kind of color, extended or not. Use the subtype when you want to
restrict the range (ie Color or Extended_Color).
BTW: as a point of Ada style, do NOT name scalar types as a plural noun.
Do not say
type Colors is...
say
type Color is ...
The reason is that the object of the type holds only a single value, for example
The_Color : Colors;
doesn't make any sense. The proper way is
The_Color : Color;
Reserve plural names for objects of a composite type, for example
type Color_Array is array (Positive range <>) of Color;
Colors : Color_Array := (...);
The plural name makes sense here, because Colors is an object that contains
multiple values.
Hope that helps,
Matt
--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Two simple language questions
1998-01-07 0:00 Chip Richards
1998-01-07 0:00 ` Matthew Heaney
@ 1998-01-07 0:00 ` Robert Dewar
1998-01-07 0:00 ` Tucker Taft
1998-01-07 0:00 ` Dale Stanbrough
3 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1998-01-07 0:00 UTC (permalink / raw)
Chip asks
<<First: It says in the Intro to Ada 9X, in Chapter 2, "General access types can
also be used to program static ragged arrays as for example a table of
messages of different lengths." Now, I know this has been asked before, some
time ago, but I cannot seem to locate the text of the exchange. What I want
to know is, exactly what is the syntax for doing this? I've so far used
allocators, i.e.:
strs: StringArr :=
(
new string'("Line 1"),
new string'("Line 2"),
...
new string'("Line the Last")
);
Which works great, but isn't exactly the "static" ragged arrays mentioned in
the intro. What I want is something like "Line 1"'access, but of course that
doesn't work, nor have any of the sixteen-umpty variations that I've tried.
The RM just said that in X'access, X must be an aliased view of an object.
Well, hmph. Hep me, hep me, please.
>>
I expect others will answer with the obvious use of 'Access on objects so
I won't repeat that here. I will note that you can find a nice example of
this use in the gnatcmd utility in the GNAT sources. In general I think
those who like learning from examples :-) may find it useful to browse the
GNAT sources for useful stuff. The stand alone utilities like gnatcmd are
quite accessible, as are many of the runtime library routines. Even the
main compiler code is reasonable to get into.
Second, the form with allocators, if used at the outer level in a library
package, does not seem particularly more or less static than the form with
the use of 'Access.
Here I am using static in an informal sense to mean no elaboration code at
runtime, which is not quite what static means in Ada, and is also, a bit
surprisingly, not quite what Preelaborate means.
A compiler might or might not generate code for the use quoted above of
allocators, it is certainly quite reasonable that it should not.
A compiler might or might not generate code for the use of 'Access in an
aggregate, it is certainly quite reasonable that it should not.
So don't jump to conclusions too fast :-)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Two simple language questions
1998-01-07 0:00 Chip Richards
1998-01-07 0:00 ` Matthew Heaney
1998-01-07 0:00 ` Robert Dewar
@ 1998-01-07 0:00 ` Tucker Taft
1998-01-07 0:00 ` Dale Stanbrough
3 siblings, 0 replies; 7+ messages in thread
From: Tucker Taft @ 1998-01-07 0:00 UTC (permalink / raw)
Chip Richards (chipr@niestu.com) wrote:
: I hope.
: First: It says in the Intro to Ada 9X, in Chapter 2, "General access types can
: also be used to program static ragged arrays as for example a table of
: messages of different lengths." Now, I know this has been asked before, some
: time ago, but I cannot seem to locate the text of the exchange. What I want
: to know is, exactly what is the syntax for doing this? I've so far used
: allocators, i.e.:
: strs: StringArr :=
: (
: new string'("Line 1"),
: new string'("Line 2"),
: ...
: new string'("Line the Last")
: );
: Which works great, but isn't exactly the "static" ragged arrays mentioned in
: the intro.
Actually, this should work as desired if StringArr is defined
as an array of access-to-constant String. E.g.:
type String_Constant_Ptr is access constant String;
type StringArr is array(Positive range <>) of String_Constant_Ptr;
The implementation advice in the RM (13.11(24)) suggesting that
access-to-constant types need not support deallocation is meant
to encourage implementations to implement allocators for
access-to-constant types statically (presuming the initializing
value is compile-time known). In other words, new String'("Line 1")
should allocate and initialize the space at link-time, and at run-time
deliver the address of this link-time allocated and initialized
space. This is essentially identical to the implementation of string
literals in C/C++.
For what it is worth, the Ada 95 compilers based on our AdaMagic
front end (i.e. Aonix ObjectAda 7.* and Green Hills AdaMulti 95)
follow this implementation model. I'm not sure about GNAT, OCSystems,
Rational, etc.
: ...
: Chip
--
-Tucker Taft stt@inmet.com http://www.inmet.com/~stt/
Intermetrics, Inc. Burlington, MA USA
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Two simple language questions
1998-01-07 0:00 Chip Richards
` (2 preceding siblings ...)
1998-01-07 0:00 ` Tucker Taft
@ 1998-01-07 0:00 ` Dale Stanbrough
3 siblings, 0 replies; 7+ messages in thread
From: Dale Stanbrough @ 1998-01-07 0:00 UTC (permalink / raw)
In article <68uq34$7tk@tomquartz.niestu.com> Chip Richards,
chipr@niestu.com writes:
> strs: StringArr :=
> (
> new string'("Line 1"),
> new string'("Line 2"),
> ...
> new string'("Line the Last")
> );
re ragged arrays..
not as easy as in C . The solution is to simulate what the C compiler
does.
A snippet from an ada95 update i did to the anna toolkit prolog
interpreter.
type Acc_String is access constant String;
Var_Space_Str : aliased constant String := "out of variable name
space";
Wierd_Ch_Str : aliased constant String := "illegal character in
input";
Fault_Str : aliased constant String := "internal error";
In_File_Depth_Str : aliased constant String := "nesting-level of
'see' is to deep";
Out_File_Depth_Str : aliased constant String := "nesting-level of
'tell' is to deep";
File_Name_Str : aliased constant String := "file-name error";
File_Status_Str : aliased constant String := "status error";
Init_Str : aliased constant String := "error during
initialization";
Err_Array : array (Error_Type) of Acc_String := (
Arity_Error => Arity_Str'Access,
Assert_Error => Assert_Str'Access,
Atom_Space_Error => Atom_Space_Str'Access,
Bad_Cdd_Error => Bad_Cdd_Str'Access,
Bad_Char_Error => Bad_Char_Str'Access,
Bad_Delim_Error => Bad_Delim_Str'Access,
Bad_Exp_Error => Bad_Exp_Str'Access,
Call_Error => Call_Str'Access,
Clause_Error => Clause_Str'Access,
Comment_Error => Comment_Str'Access,
Dale
^ permalink raw reply [flat|nested] 7+ messages in thread