mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-06 14:52:03 +01:00
Updated wiki-formatting for Eiffel code.
Updated wikipage Manifest array. (Signed-off-by:jocelyn). git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@2037 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -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 <code>1</code> 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 <code><<1, 4, 9, 16, 25>></code>. The lower index is always <code>1</code> and the upper index is the number of items, <code>5</code> 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 <code>ARRAY [T]</code> where <code>T</code> is a type to which all the elements conform, <code>INTEGER</code> in the previous example. In case of a possible ambiguity you can make the type explicit, as in <code>{ARRAY [COMPARABLE]} <<7, "Eiffel">></code>, where both <code>INTEGER</code>, the type of <code>7</code>, and <code>STRING</code>, the type of <code>"Eiffel"</code>, conform to <code>COMPARABLE</code>.
|
||||
|
||||
== Typed manifest array ==
|
||||
== What are manifest arrays good for? ==
|
||||
|
||||
A typed manifest array has an explicit Manifest_array_type that should resolve to an <e>ARRAY</e> 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
|
||||
|
||||
<e>
|
||||
{ARRAY [COMPARABLE]} <<7, "beers">>
|
||||
</e>
|
||||
```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 <code>squares</code> through
|
||||
|
||||
<e>
|
||||
create x.make (1, 0)
|
||||
x.force (7, 1)
|
||||
x.force ("beers", 2)
|
||||
</e>
|
||||
```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 `<<True, False, False>>` 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]`.
|
||||
```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 <code><<>></code>: <code>ARRAY [NONE]</code>. (In the following cases we assume the array is not empty.)
|
||||
|
||||
* If all elements are of the same exact type <code>T</code>: <code>ARRAY [T]</code>.
|
||||
|
||||
* If the types of all elements all conform to a type <code>T</code>: <code>ARRAY [T]</code>. Note that in this case <code>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: <code>ARRAY [ANY]</code>.
|
||||
|
||||
As an example of the third case (conformance of all elements to one of them), assume <code>POLYGON</code> and <code>CIRCLE</code> both conform to <code>FIGURE</code>. Then the manifest array <code><<a_polygon, a_figure, a_circle>></code>, with <code>a_polygon of type <code>POLYGON</code> and so on, is <code>ARRAY [FIGURE]</code>.
|
||||
|
||||
== Type of a manifest array: the explicit case ==
|
||||
|
||||
With the preceding rule, the type of <code><<7, "Eiffel">></code> is the most general possible one, <code>ARRAY [ANY]</code>, since <code>INTEGER</code> and <code>STRING</code> 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 <code>{ARRAY [T]} <<element1, element2, ...>></code> the types of all elements must conform to <code>T</code>.
|
||||
|
||||
As another example, with
|
||||
|
||||
```eiffel
|
||||
figures: ARRAY [FIGURE]
|
||||
```
|
||||
|
||||
you cannot assign <code><<a_polygon, a_circle>></code> to <code>figures</code> since the type of the manifest array is <code>ARRAY [ANY]</code>. To make this assignment possible, use an explicit type:
|
||||
|
||||
```eiffel
|
||||
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 <code>ARRAY [NONE]</code>. For example, with <code>figures</code> declared as above:
|
||||
|
||||
```eiffel
|
||||
figures := {ARRAY [FIGURE]} <<>>
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user