Skip to content

Add getWithDefault and getOrThrow to Belt.HashMap - #8926

Draft
cknitt wants to merge 1 commit into
masterfrom
belt-hashmap-get-with-default
Draft

cknitt wants to merge 1 commit into
masterfrom
belt-hashmap-get-with-default

Conversation

@cknitt

@cknitt cknitt commented Oct 19, 2026

Copy link
Copy Markdown
Member

Adds getWithDefault and getOrThrow to Belt.HashMap, Belt.HashMap.Int and Belt.HashMap.String. They walk the bucket directly (no option allocated) and getOrThrow throws Not_found, like the other Belt maps.

The issue asks for getOr; Belt names this lookup getWithDefault everywhere (Option, Map, MutableMap) and has no getOr, so the new function follows Belt.

Fixes #8381.

New tests: mocha cases in tests/belt_tests/src/belt_hashmap_test.res (generic map, a map whose keys all collide into one bucket, Int and String variants) and docstring examples for each new function.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • Add getWithDefault and getOrThrow to Belt.HashMap, Belt.HashMap.Int and Belt.HashMap.String.

Semantic Diff

1 commit · Full page

✏️ Add getWithDefault and getOrThrow to Belt.HashMap

43bce48 · cknitt + Claude · Oct 19, 2026, 19:55 UTC · edited

Belt.HashMap, Belt.HashMap.Int and Belt.HashMap.String gain getWithDefault and getOrThrow, the same lookups Belt.Map and Belt.MutableMap already have. They walk the bucket directly, so a hit allocates no option; getOrThrow throws Not_found like the other Belt maps.

let m = Belt.HashMap.Int.fromArray([(1, "a")])
m->Belt.HashMap.Int.getWithDefault(2, "z") == "z"
m->Belt.HashMap.Int.getOrThrow(1) == "a"

Review

  • Why getWithDefault and not getOr, as the issue title says? Belt names this lookup getWithDefault everywhere (Option, Map, MutableMap and their Int/String variants) and has no getOr; the new functions follow Belt. getOrThrow is the name Belt already uses, without a deprecated getExn alias for a new API.

@pkg-pr-new

pkg-pr-new Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8926

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@8926

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8926

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8926

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8926

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8926

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8926

commit: 43bce48

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T06:08:37.857188Z 1e7faef Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 1e7faef13f

ℹ️ 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".

Belt.HashMap, Belt.HashMap.Int and Belt.HashMap.String gain
getWithDefault and getOrThrow, the same lookups Belt.Map and
Belt.MutableMap already have. They walk the bucket directly, so a hit
allocates no option; getOrThrow throws Not_found like the other Belt
maps.

    let m = Belt.HashMap.Int.fromArray([(1, "a")])
    m->Belt.HashMap.Int.getWithDefault(2, "z") == "z"
    m->Belt.HashMap.Int.getOrThrow(1) == "a"

Review: Why getWithDefault and not getOr, as the issue title says? Belt
    names this lookup getWithDefault everywhere (Option, Map, MutableMap
    and their Int/String variants) and has no getOr; the new functions
    follow Belt. getOrThrow is the name Belt already uses, without a
    deprecated getExn alias for a new API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the belt-hashmap-get-with-default branch from 1e7faef to 43bce48 Compare October 29, 2026 13:17

This branch has not been deployed

No deployments
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.

Add getOr/getOrThrow API for Belt.HashMap

1 participant