Skip to content

Commit

Permalink
Add Get method to Iterator
Browse files Browse the repository at this point in the history
Necessary for C++ interoperability with iterators.
  • Loading branch information
Pixep committed Oct 1, 2022
1 parent 91d9709 commit c90d7ba
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions proposals/p1885.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Alternatives considered](#alternatives-considered)
- [Indexable](#indexable)
- [Using interface type parameters](#using-interface-type-parameters)
- [Relying only on `Next()` from `Iterator` to move and get value](#relying-only-on-next-from-iterator-to-move-and-get-value)
- [Other](#other)

<!-- tocstop -->
Expand Down Expand Up @@ -275,7 +276,8 @@ definition. The usage highlighted here is hypothetical.
// Basic immutable iterator
interface Iterator {
let ElementType:! Type;
fn Next[me: Self]() -> Optional(ElementType);
fn Get[me: Self]() -> Optional(ElementType);
fn Next[addr me: *Self]() -> Optional(ElementType);
}
// Iterable type
Expand All @@ -294,10 +296,14 @@ class MyContainer() {
class IteratorType { // Associated type as member type
impl as Iterator where .ElementType = i32 {
...
fn Get[me: Self]() -> Optional(ElementType) {
return if atEnd then .Empty else currItem;
}
fn Next[addr me: *Self]() -> Optional(ElementType) {
// move to next item position
...
return if atEnd then .Empty else currItem;
return me->Get();
}
}
}
Expand Down Expand Up @@ -407,6 +413,16 @@ Interfaces can use
as `interface Iterator(ItemType:! Type)` instead of associated types. This would
however be inconvenient as they require specifying the type again to be used.

### Relying only on `Next()` from `Iterator` to move and get value

The `Iterator` interface currently exposes both `Get` and `Next` methods. While
`Next` could be sufficient to support iterating and retrieving data (returned),
this makes the C++ interoperability complex, by not allowing straightforward
dereferencing (C++'s `operator*`) without side-effects.

Exposing a `Get` method allow for a simple mapping of iterator dereferencing,
which is needed to provide proper interoperability.

### Other

- New keyword or syntax
Expand Down

0 comments on commit c90d7ba

Please sign in to comment.