[GAP] Record aggregates with unknown components
Francisco J. Montoya
fmontoya at dif.um.es
Mon Mar 13 16:43:03 CET 2006
Dear Ada programmers/coders/teachers/researchers/lovers:
Just a simple question from an Ada newbie. As a first example on
generics I showed to my students the classic example of a generic
package that defines the type set for ordinal types as an array of
boolean values. After that, I asked them for writing another generic
package to define the type set for any (non-limited) type assuming that
there exists a known upper bound on the number of elements that any set
may contain and an order relationship defined for the base type. This
generic package should look like the following:
generic
type T is private;
with function "<=" (left, right : in T) return Boolean is <>;
MAX_LENGTH : in Positive := 100;
package general_sets is
...
end general_sets;
The idea is that they should define the type set by means of a simple
record with two components, the first one would be an array where the
elements belonging to the set are stored in their corresponding order,
and the second one would be the number of significant elements in the
former array, just like:
type T_elements is array (1 .. MAX_LENGTH) of T;
type set is record
elements : T_elements;
num_elements : Natural := 0;
end record;
So far, so good, but my problem arose (or "raised") when I tried to
define the rest of elements associated to this type in the same way I
did for ordinal types sets. In particular, I got stuck with the EMPTY
constant. In the case of ordinal types sets it is straightforward to
define this constant as:
EMPTY : constant set := set'( others => False );
, but in the former case I cannot find a way to make a definition like:
EMPTY : constant set := set'( ( others => any ), 0 );
because I don't know at that point "any" value of type T that can be
used, though "any" would be fine since it's not relevant. I've been
reading the Annotated Ada Reference Manual and it seems to me that "all"
composite value are needed in any aggregate definition (Section 4.3.1,
paragraph 9).
Cannot this constant be defined in any way? So far I've found just two
choices:
a) Defining EMPTY as a function without parameters.
b) Defining EMPTY as a procedure with one "out" parameter set to empty
in the procedure body.
Maybe choice a) could be fine in this case, but in general, where one
may want define "aliased" constants this could be not so fine.
Any idea on this?
Thank you very much for your attention, and my apologies if this is a
stupid question (it probably is),
--
Francisco J. Montoya
Depto. Informatica y Sistemas, Facultad de Informatica
(E-30071 Campus de Espinardo) Universidad de Murcia (Spain)
Voice: +34 968 364620, Fax: +34 968 364151, e-mail: fmontoya at dif.um.es
More information about the GAP
mailing list