Implement map destructuring in let bindings#82
Conversation
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
`[let {name age} m]` map destructuring worked on the legacy interpreter
(Param::MapDestructure in bind_param, including per-key defaults) but the
default EIR VM backend silently bound nothing — lower_let had a TODO arm.
- eir/lower.rs: lower_map_destructure binds each key via Op::Field
(Selector::Name works on maps in the VM). A distinct per-key value is a
default expression, lowered behind a `contains?` branch so it is only
evaluated on the miss path; a missing key with no default binds unit,
matching the interpreter's `None => Value::Unit`.
- check/mod.rs: bind_map_destructure binds each name to the map's value
type (`Map Keyword v` → `v`) instead of leaving names unbound, and
unifies any default expression against that element type.
- backend_parity.rs: shorthand, present-key-wins-over-default, and
missing-key-binds-unit cases, all asserted equal across backends.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzaPzShDQzi6KqNoWqCbE5
8f57ad3 to
90aa035
Compare
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
What shippedYou can now extract multiple values from a map in a single binding step. For example, binding Plain-English summary generated by Choji from this pull request. |
Summary
Add support for map destructuring patterns in
letbindings, allowing users to extract multiple values from a map in a single binding. This feature is implemented in both the type checker and EIR lowering, with comprehensive test coverage.Changes
Type Checker (
crates/loon-lang/src/check/mod.rs)bind_map_destructuremethod to handle type inference for map destructuring patterns{name name}(no default) and explicit defaults{name default_expr}EIR Lowering (
crates/loon-lang/src/eir/lower.rs)lower_let_bindingto use pattern matching instead of simple symbol checklower_map_destructuremethod to generate efficient IR for map destructuringOp::Fieldthat returns unit for missing keysOp::Builtin(Contains)to avoid evaluating defaults unnecessarilyTests (
crates/loon-lang/tests/backend_parity.rs){name name age age}binds keys to their values()Implementation Details
The lowering strategy uses conditional branching for patterns with defaults to ensure side effects in default expressions only occur when needed. For shorthand patterns without defaults, a simpler single-operation approach is used since
Op::Fieldnaturally returns unit for missing keys, matching the legacy interpreter's semantics.https://claude.ai/code/session_01JzaPzShDQzi6KqNoWqCbE5