diff --git a/documentation/18.01/eiffel/Language_reference/quick-reference-eiffel-programming-language/Expressions/Manifest-array.wiki b/documentation/18.01/eiffel/Language_reference/quick-reference-eiffel-programming-language/Expressions/Manifest-array.wiki index 48444d6e..4db08d68 100644 --- a/documentation/18.01/eiffel/Language_reference/quick-reference-eiffel-programming-language/Expressions/Manifest-array.wiki +++ b/documentation/18.01/eiffel/Language_reference/quick-reference-eiffel-programming-language/Expressions/Manifest-array.wiki @@ -2,30 +2,81 @@ [[Property:weight|0]] [[Property:title|Manifest array]] -Manifest arrays provide a shortcut to create and initialize an array object using a single expression rather than a creation instruction followed by a sequence of insertions of new elements to the array. The lower index of a manifest array is always 1 and the upper index is the number of items. +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. -[[Eiffel%20Programming%20Language%20Syntax#Manifest_arrays|Manifest arrays]] come in two flavors: typed and untyped with slightly different validity rules and run-time behavior. +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. -== Typed manifest array == +== What are manifest arrays good for? == -A typed manifest array has an explicit Manifest_array_type that should resolve to an ARRAY type. It is used to create an object to be filled with specified elements. Every element should be compatible with the specified element type. For example, the expression +Use a manifest array to initialize an element by simply listing its initial elements. For example, with the declaration - - {ARRAY [COMPARABLE]} <<7, "beers">> - +```eiffel + squares: ARRAY [INTEGER] +``` -creates an array of type `ARRAY [COMPARABLE]` with 2 elements: `7` and `"beers"` and is therefore equivalent to the value of an imaginary variable `x` of type `ARRAY [COMPARABLE]` initialized as follows: +you can initialize squares through - - create x.make (1, 0) - x.force (7, 1) - x.force ("beers", 2) - +```eiffel + squares := <<1, 4, 9, 16, 25>> +``` -It would be an error to use `default_pointer` instead of `"beers"` because the type `POINTER` is not `COMPARABLE`. Similarly, one cannot specify `LIST` or a formal generic instead of `ARRAY`. Also, the explicit manifest array type cannot be `separate`. +This is simpler than the alternative, which would be to create the array explicitly and give a value to every element in turn: +```eiffel + create squares .make_filled (0, 1, 5) + -- Arguments to `make_filled' are: default value, lower bound, upper bound. + squares [1] := 1 ; squares [2] := 4 ; squares [3] := 9 + squares [4] := 16 ; squares [5] := 25 +``` -== Untyped manifest array == +The first form, with the manifest array, is shorter, but the effect is the same. -An untyped manifest array has no Manifest_array_type part. As a result, its type is computed from the type of specified elements as a [[Types#Common ancestor types|common ancestor type]]. So, the type of the expression `<<7, "beers">>` would be `ARRAY [ANY]` rather than `ARRAY [COMPARABLE]` because types of the elements do not conform to each other. On the other hand, the expression `<>` has type `ARRAY [BOOLEAN]`. +Manifest arrays are normal arrays, not restricted in any way. You can for example add elements to them, as in -Because there is always a common ancestor type for manifest array elements, the untyped manifest array is always valid as soon as all its elements are valid. The empty untyped manifest array has type `ARRAY [NONE]`. \ No newline at end of file +```eiffel + squares.force (36, 6) + -- Arguments to `force' are: value, position. +``` + +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 <>, 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: + +```eiffel +{ARRAY [COMPARABLE]} <<7, "Eiffel">> +``` + +The rule in such a case is that in {ARRAY [T]} <> the types of all elements must conform to T. + +As another example, with + +```eiffel +figures: ARRAY [FIGURE] +``` + +you cannot assign <> to figures since the type of the manifest array is ARRAY [ANY]. To make this assignment possible, use an explicit type: + +```eiffel +figures := {ARRAY [FIGURE]} <> +``` + +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: + +```eiffel +figures := {ARRAY [FIGURE]} <<>> +```