Adopt ref in Core.Iterate.Next

Pull request

Table of contents

Abstract

Change the cursor parameter in Core.Iterate.Next to be a ref parameter instead of a pointer parameter.

Problem

According to p001885, Core.Iterate.Next is declared as follows:

  fn Next[self: Self](cursor: CursorType*) -> Optional(ElementType);

Aside from the old self syntax, this uses pass-by-pointer to pass a mutable reference. This stopped being idiomatic Carbon when ref patterns were added, but we still require it when interacting with this core language feature.

Background

Proposal p001885 added the Iterate interface.

Proposal p002006 affirmed that Carbon does not have reference types, but does have reference expressions.

Proposal p005434 added support for ref patterns to match reference expressions. These replaced pass-by-pointer as the idiomatic way to pass a handle to a mutable object into a function.

Proposal

Change the declaration of Core.Iterate.Next to:

  fn Next(self, ref cursor: CursorType) -> Optional(ElementType);

Rationale

Future work

We should consider making Core.Iterate a form-generic interface, and making the ElementType instead be an associated form, with a suitable change to return a form-generic Optional.

Alternatives considered

None.