Skip to content

refactor: collapse ObjectKind accessor boilerplate into kind_accessor! macro#187

Draft
pmatos wants to merge 1 commit into
mainfrom
claude/loving-mccarthy-500frp
Draft

refactor: collapse ObjectKind accessor boilerplate into kind_accessor! macro#187
pmatos wants to merge 1 commit into
mainfrom
claude/loving-mccarthy-500frp

Conversation

@pmatos

@pmatos pmatos commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • Introduces a kind_accessor! declarative macro in src/interpreter/types.rs that generates the standard ObjectKind accessor pattern (Some when variant matches, None otherwise) for both immutable and mutable forms.
  • Replaces ~30 near-identical hand-written method bodies (17 accessor pairs/singles) with 17 macro invocations.
  • Retains 3 hand-written special cases that don't fit the pattern: intl_data (boxed inner value needs .as_ref()), finalization_registry{,_mut} (struct variant with two named fields, tuple return), and shadow_realm_id (Copy return type, not a reference).
  • Adds 17 unit tests in kind_accessor_tests module covering: Some/None discrimination by kind, mutable access enables mutation, and mutual exclusion between different kinds.

Motivation

The audit identified this as the highest-ROI refactoring: every new ObjectKind variant previously required manually writing two structurally identical methods. A missed _mut accessor was easy to overlook and would only be caught at a call site. The macro makes the pair atomic — you declare the variant once and both accessors appear.

Test plan

  • cargo test kind_accessor — all 17 new tests pass
  • cargo test — full suite (181 tests) passes with no regressions
  • cargo build — clean compile, no warnings

https://claude.ai/code/session_012MbKtDLjpVxxY655NVhTSE


Generated by Claude Code

…! macro

The 17 standard ObjectKind accessor pairs (and immutable-only variants)
all followed an identical `if let ObjectKind::Variant(ref x) = self.kind
{ Some(x) } else { None }` pattern, repeated verbatim for both the
immutable and mutable form — ~30 method bodies in total.

Introduces a `kind_accessor!` declarative macro with two arms:
- `(name, mut_name, Variant, Type)` for immutable+mutable pairs
- `(name, Variant, Type)` for immutable-only

The three special-case methods that don't fit the pattern are kept as
hand-written impls with a comment explaining why:
- `intl_data` — boxed inner value requires an extra `.as_ref()` deref
- `finalization_registry{,_mut}` — struct variant with two named fields
- `shadow_realm_id` — Copy return type, not a reference

Adds 17 unit tests (kind_accessor_tests module) covering: Some/None
discrimination by kind, mutable access enables mutation, and mutual
exclusion between different kinds.

🤖 Generated with Claude Code
Claude-Session: https://claude.ai/code/session_012MbKtDLjpVxxY655NVhTSE
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