feat(stdlib): add list get/get?/index/index? accessors 🎯#169
Merged
Conversation
Accept BigInt in the usize extractor (fixes get? crash on huge indices) and clean up NumberToUsizeError messages. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Follow-up to #167, which added
list.get?. A Codex review of that PR foundget?crashed on large indices: it took ani64, and the#[function]macro'sinteger extractor rejects
BigIntvalues before the function body runs — eventhough a
BigInthas static typeIntand passes call resolution. So[1].get?(10^40)raisedarg 1: expected int, got objectinstead of behavinglike 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
getusizeget?usizeNoneindexBigIntindex?BigIntNoneThe argument type decides negative handling:
get/get?take ausize, so anegative index is a conversion error;
index/index?take a signedBigIntand wrap from the end like the
[]operator. The?suffix decidesout-of-bounds handling (error vs
None).indexis the named form of[];index?is its non-throwing twin (there is no[]?).Primary changes
ndc_macros/src/vm_convert.rs— theusizeargument extractor nowconverts via
to_number()+TryFrom<Number> for usize, accepting bothIntand
BigIntand raising a clear error on negative/oversized values. This iswhat lets
get/get?reject negatives at the conversion layer, and it fixesthe
BigIntcrash. It also fixes a latent bug: the old*i as usizesilentlywrapped a negative index to a huge
usizefor everyusizebuiltin(
insert,truncate, …).ndc_stdlib/src/list.rs— addsget,index,index?and reworksget?onto the
usizesignature, plus a smallwrap_indexhelper for the signedvariants.
ndc_core/src/num.rs— clearerNumberToUsizeErrormessages (the oldBigIntone leaked internal conversion jargon).option.mdandlist.mddocument the four accessors.601_stdlib_list/004–012cover the success and error paths,including the previously-crashing
get?(-1)andget?(10^40).Notes for reviewers
usize-extractor change is the widest-reaching part: it affects allusizebuiltins, so error messages for bad size/index arguments changed. Thefull 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.🤖