Skip to content

Commit 941b0a8

Browse files
committed
describe collections
1 parent 05691de commit 941b0a8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
# Collections
2+
3+
When using formality, it's best to use the following collection types:
4+
5+
* for sequences, use the standard `Vec` type
6+
* `formality_core::Set<T>` -- equivalent to `BTreeSet` but shorter. We use a `BTreeSet` because it has deterministic ordering for all operations.
7+
* `formality_core::Map<K, V>` -- equivalent to `BTreeMap` but shorter. We use a `BTreeMap` because it has deterministic ordering for all operations.
8+
9+
## Macros
10+
11+
We also define macros:
12+
13+
* `seq![...]` -- equivalent to `vec![]` but permits flattening with `..` notation, as described below
14+
* `set![...]` -- like `seq!`, but produces a `Set`
15+
16+
In these macros you can "flatten" things that support `IntoIterator`, so `set![..a, ..b]` will effectively perform a set union of `a` and `b`.
17+
18+
## Casting between collections and tuples
19+
20+
It is possible to upcast from variable tuple types to produce collections:
21+
22+
* A `Vec<E1>` can be upcast to a `Vec<E2>` if `E1: Upcast<E2>`.
23+
* A `Set<E1>` can be upcast to a `Set<E2>` if `E1: Upcast<E2>`.
24+
* Tuples of elements (e.g., `(E1, E2)` or `(E1, E2, E3)`) can be **upcast** to sets up to a fixed arity.
25+
* Sets and vectors can be **downcast** to `()` and `(E, C)`, where `()` succeeds only for empty collections, and `(E, C)` extracts the first element `E` and a collection `C` with all remaining elements (note that elements in sets are always ordered, so the first element is well defined there). This is useful when writing judgment rules that operate over sequences and sets.

0 commit comments

Comments
 (0)