Skip to content

feat(stdlib): add list get? and unwrap_or 🎁#167

Merged
timfennis merged 1 commit into
masterfrom
feature/list-get-unwrap-or
Jun 2, 2026
Merged

feat(stdlib): add list get? and unwrap_or 🎁#167
timfennis merged 1 commit into
masterfrom
feature/list-get-unwrap-or

Conversation

@timfennis
Copy link
Copy Markdown
Owner

@timfennis timfennis commented Jun 2, 2026

Adds list.get?(index) (safe indexed lookup returning an option) and unwrap_or.

🤖

@timfennis timfennis force-pushed the feature/list-get-unwrap-or branch from 67096aa to 47eeb98 Compare June 2, 2026 04:34
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 67096aabba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ndc_stdlib/src/list.rs
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timfennis timfennis force-pushed the feature/list-get-unwrap-or branch from 47eeb98 to 9bb43ee Compare June 2, 2026 05:21
@timfennis timfennis enabled auto-merge (squash) June 2, 2026 05:21
@timfennis timfennis merged commit 4ed65cc into master Jun 2, 2026
1 check passed
@timfennis timfennis deleted the feature/list-get-unwrap-or branch June 2, 2026 05:22
@timfennis timfennis changed the title feat(stdlib): add list get? and unwrap_or 🎁 feat(stdlib): add list get/get?/index/index? accessors 🎯 Jun 2, 2026
@timfennis timfennis changed the title feat(stdlib): add list get/get?/index/index? accessors 🎯 feat(stdlib): add list get? and unwrap_or 🎁 Jun 2, 2026
timfennis added a commit that referenced this pull request Jun 2, 2026
## Context

Follow-up to #167, which added `list.get?`. A Codex review of that PR
found
`get?` crashed on large indices: it took an `i64`, and the `#[function]`
macro's
integer extractor rejects `BigInt` values *before* the function body
runs — even
though a `BigInt` has static type `Int` and passes call resolution. So
`[1].get?(10^40)` raised `arg 1: expected int, got object` instead of
behaving
like the advertised safe lookup.

Fixing that cleanly meant settling how list element access should work
in
general, which is now a 2×2 matrix.

## Element accessors

| function | index argument | negative index | out of bounds |
|----------|----------------|----------------|---------------|
| `get`    | `usize`  | error            | error  |
| `get?`   | `usize`  | error            | `None` |
| `index`  | `BigInt` | wraps from end   | error  |
| `index?` | `BigInt` | wraps from end   | `None` |

The argument type decides negative handling: `get`/`get?` take a
`usize`, so a
negative index is a conversion error; `index`/`index?` take a signed
`BigInt`
and wrap from the end like the `[]` operator. The `?` suffix decides
out-of-bounds handling (error vs `None`). `index` is the named form of
`[]`;
`index?` is its non-throwing twin (there is no `[]?`).

## Primary changes

- **`ndc_macros/src/vm_convert.rs`** — the `usize` argument extractor
now
converts via `to_number()` + `TryFrom<Number> for usize`, accepting both
`Int`
and `BigInt` and raising a clear error on negative/oversized values.
This is
what lets `get`/`get?` reject negatives at the conversion layer, and it
fixes
the `BigInt` crash. It also fixes a latent bug: the old `*i as usize`
silently
wrapped a negative index to a huge `usize` for **every** `usize` builtin
  (`insert`, `truncate`, …).
- **`ndc_stdlib/src/list.rs`** — adds `get`, `index`, `index?` and
reworks `get?`
onto the `usize` signature, plus a small `wrap_index` helper for the
signed
  variants.
- **`ndc_core/src/num.rs`** — clearer `NumberToUsizeError` messages (the
old
  `BigInt` one leaked internal conversion jargon).
- **Docs** — `option.md` and `list.md` document the four accessors.
- **Tests** — `601_stdlib_list/004`–`012` cover the success and error
paths,
  including the previously-crashing `get?(-1)` and `get?(10^40)`.

## Notes for reviewers

- The `usize`-extractor change is the widest-reaching part: it affects
all
`usize` builtins, so error messages for bad size/index arguments
changed. The
  full test suite passes (427 tests).
- `index`/`index?` are list-only element access (no range/slice
support); the
  `[]` operator still owns slicing and other container types.
- <!-- Reviewer: happy with `get`/`index` as the names for the usize vs
  signed-BigInt pair? -->

🤖

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant