merged from 18.01

git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@2044 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
eiffel-org
2018-07-04 08:50:09 +00:00
parent a8f3738506
commit 9a546a8385
3 changed files with 74 additions and 18 deletions

View File

@@ -2,30 +2,84 @@
[[Property:weight|0]] [[Property:weight|0]]
[[Property:title|Manifest array]] [[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 `<<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 <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> ```eiffel
{ARRAY [COMPARABLE]} <<7, "beers">> squares: ARRAY [INTEGER]
</e> ```
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
<e> ```eiffel
create x.make (1, 0) squares := <<1, 4, 9, 16, 25>>
x.force (7, 1) ```
x.force ("beers", 2)
</e>
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
-- 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
```
== 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
-- 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:
```eiffel
{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
```eiffel
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:
```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 `ARRAY [NONE]`. For example, with `figures` declared as above:
```eiffel
figures := {ARRAY [FIGURE]} <<>>
```

View File

@@ -2,7 +2,7 @@
[[Property:link_title|Quick Reference]] [[Property:link_title|Quick Reference]]
[[Property:weight|4]] [[Property:weight|4]]
[[Property:uuid|4f61365d-59f6-a394-678b-144bad8ec12f]] [[Property:uuid|4f61365d-59f6-a394-678b-144bad8ec12f]]
The Quick Reference to the Eiffel programming language provides an informal guide to the syntax and reserved words of the language. The Eiffel programming language is described in detail in the ISO/ECMA standard document, available [http://www.ecma-international.org/publications/standards/Ecma-367.htm online]. The Quick Reference to the Eiffel programming language provides an informal guide to the syntax and reserved words of the language. The Eiffel programming language is described in detail in the '''ISO/ECMA''' standard document, available [http://www.ecma-international.org/publications/standards/Ecma-367.htm online].
Sometimes there are differences between the language as defined by the standard and that which is implemented by Eiffel Software. These differences are documented in the online documentation. Sometimes there are differences between the language as defined by the standard and that which is implemented by Eiffel Software. These differences are documented in the online documentation.

View File

@@ -1,3 +1,5 @@
[[Property:modification_date|Thu, 21 Jun 2018 09:11:53 GMT]]
[[Property:publication_date|Thu, 21 Jun 2018 09:10:31 GMT]]
[[Property:link_title|Reference]] [[Property:link_title|Reference]]
[[Property:title|EiffelStudio Reference]] [[Property:title|EiffelStudio Reference]]
[[Property:weight|3]] [[Property:weight|3]]