Manifest array

A manifest array is an expression denoting an array by simply listing its elements, as in <<1, 4, 9, 16, 25>>. The lower index is always 1 and the upper index is the number of items, 5 in this example.

The type of a manifest array is always ARRAY [T] where T is a type to which all the elements conform, INTEGER in the previous example. In case of a possible ambiguity you can make the type explicit, as in {ARRAY [COMPARABLE]} <<7, "Eiffel">>, where both INTEGER, the type of 7, and STRING, the type of "Eiffel", conform to COMPARABLE.

What are manifest arrays good for?

Use a manifest array to initialize an element by simply listing its initial elements. For example, with the declaration

squares: ARRAY [INTEGER]

you can initialize squares through

squares := <<1, 4, 9, 16, 25>>

This is simpler than the alternative, which would be to create the array explicitly and give a value to every element in turn: -- Arguments to `make_filled` are: default value, lower bound, upper bound. create squares.make_filled (0, 1, 5) squares [1] := 1 squares [2] := 4 squares [3] := 9 squares [4] := 16 squares [5] := 25

The first form, with the manifest array, is shorter, but the effect is the same.

Manifest arrays are normal arrays, not restricted in any way. You can for example add elements to them, as in

-- Arguments to `force` are: value, position. squares.force (36, 6)

which will resize the array to bounds 1 and 6.

Type of a manifest array: the implicit case

If you do not explicitly specify an array type, the type of the manifest array is as follows:

  • For an empty manifest array <<>>: ARRAY [NONE]. (In the following cases we assume the array is not empty.)
  • If all elements are of the same exact type T: ARRAY [T].
  • If the types of all elements all conform to a type T: ARRAY [T]. Note that in this case T is unique since two different types cannot conform to each other. (The preceding case, all types identical, is a special case of this one, since a type conforms to itself.)
  • Otherwise: ARRAY [ANY].

As an example of the third case (conformance of all elements to one of them), assume POLYGON and CIRCLE both conform to FIGURE. Then the manifest array <<a_polygon, a_figure, a_circle>>, with a_polygon of type POLYGON and so on, is ARRAY [FIGURE].

Type of a manifest array: the explicit case

With the preceding rule, the type of <<7, "Eiffel">> is the most general possible one, ARRAY [ANY], since INTEGER and STRING do not conform to each other (either way). If you are not happy with this default type, you can make the array type explicit by writing it in braces:

{ARRAY [COMPARABLE]} <<7, "Eiffel">>

The rule in such a case is that in {ARRAY [T]} <<element1, element2, ...>> the types of all elements must conform to T.

As another example, with

figures: ARRAY [FIGURE]

you cannot assign <<a_polygon, a_circle>> to figures since the type of the manifest array is ARRAY [ANY]. To make this assignment possible, use an explicit type:

figures := {ARRAY [FIGURE]} <<a_polygon, a_circle>>

You can also use this form to give an explicit type to an empty array, which would otherwise be of type ARRAY [NONE]. For example, with figures declared as above:

figures := {ARRAY [FIGURE]} <<>>

cached: 11/21/2024 8:35:22.000 AM