You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit improves the handling of iteration in a couple of ways.
- First, it enables pattern matching in `for` loops and `FlatMap` (vmware-archive#784). One
can now write:
```
for ((k,v) in map) {}
```
instead of
```
for ((kv in map) { var k = kv.0}
```
and
```
(var x, var y) = FlatMap(expr)
```
instead of:
```
var xy = FlatMap(expr),
var x = xy.0
```
In general, the pattern can contain constructor names, tuples,
placeholders, and variable declarations. The pattern must be
irrefutable, i.e., any constructor name used in the pattern must be
the unique constructor for the given type.
- Second, we remove the hardwired knowledge about iterable types from
the compiler. Until now the compiler only knew how to iterate over
`Vec`, `Set`, `Map`, `Group`, and `TinySet` types. Instead we now
allow the programmer to label any extern type as iterable, meaning
that it implements `iter()` and `into_iter()` methods, that return
Rust iterators using one of two attributes:
```
#[iterate_by_ref=iter:<type>]
```
or
```
#[iterate_by_val=iter:<type>]
```
where `type` is the type yielded by the `next()` method of the iterator
The former indicates that the `iter()` method returns a by-reference
iterator, the latter indicates that `iter()` returns a by-value
iterator.
Example:
```
#[iterate_by_val=iter:('K,'V)]
extern type Map<'K,'V>
```
This feature will enable us to introduce new container types, e.g.,
immutable sets and maps in the future.
Unfortunately, it introduces one, hopefully minor, usability
regression. Since we can no longer distinguish a map from any other
container whose iterator is a 2-tuple, we cannot reliably convert
DDlog maps to/from Java maps in the FlatBuf-based API. They must
therefore be passed around as arrays of key-value tuples and converted
to/from Java maps by the client.
Future todos:
- Support pattern matching in closure arguments and the LHS of `group_by` clauses.
- Should refutable patterns be supported for `FlatMap`? This would have
the effect of filtering the flattened collection.
0 commit comments