Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

String::fold and StringView::fold (and their rev_fold variants) lacked raise? annotations, preventing exceptions from propagating through the callback function. This was inconsistent with all other collection types (Array, FixedArray, ReadOnlyArray, Iter, Iterator) which support raise? in their fold operations.

Changes

  • Added raise? to callback parameter type: (A, Char) -> A(A, Char) -> A raise?
  • Added raise? to return type: -> A-> A raise?
  • Applied to: StringView::fold, StringView::rev_fold, String::fold, String::rev_fold

Example

// Exception now propagates correctly
let result = try? "hello".fold(init=0, (acc, c) => {
  if c == 'l' {
    raise Failure("found l")
  }
  acc + 1
})
// result: Err(Failure("found l"))
Original prompt

This section details on the original issue you should resolve

<issue_title>[Consistency Review] add raise? to String::fold and StringView::fold</issue_title>
<issue_description>Current behaviors

Array, FixedArray, ReadOnlyArray, Iter, and Iterator have the following signatures for fold:

fn[A, B] Array::fold(Self[A], init~ : B, (B, A) -> B raise?) -> B raise?
fn[A, B] FixedArray::fold(Self[A], init~ : B, (B, A) -> B raise?) -> B raise?
fn[A, B] ReadOnlyArray::fold(Self[A], init~ : B, (B, A) -> B raise?) -> B raise?
fn[T, B] Iter::fold(Self[T], init~ : B, (B, T) -> B raise?) -> B raise?
fn[X, R] Iterator::fold(Self[X], init~ : R, (R, X) -> R raise?) -> R raise?

but String and StringView have these signatures:

fn[A] String::fold(String, init~ : A, (A, Char) -> A) -> A
fn[A] StringView::fold(Self, init~ : A, (A, Char) -> A) -> A

Expected behaviors

I expected String::fold and StringView::fold to have raise? so that exceptions raised by the callback are raised by fold.

Justification

Adding raise? would increase consistency with other implementations of fold and make the exception types more accurate.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…tringView::rev_fold with tests

Co-authored-by: bobzhang <747051+bobzhang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add raise? to String::fold and StringView::fold Add raise? to String::fold and StringView::fold Dec 11, 2025
Copilot AI requested a review from bobzhang December 11, 2025 15:09
@bobzhang bobzhang marked this pull request as ready for review December 11, 2025 15:33
@bobzhang bobzhang merged commit 8f83e8e into main Dec 12, 2025
13 of 14 checks passed
@bobzhang bobzhang deleted the copilot/add-raise-to-string-fold branch December 12, 2025 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Consistency Review] add raise? to String::fold and StringView::fold

2 participants