Skip to content

regexp: make Match a plain object with named captures - #95

Merged
vito merged 1 commit into
mainfrom
refactor-match-plain-object
Jun 9, 2026
Merged

vito merged 1 commit into
mainfrom
refactor-match-plain-object

Conversation

@vito

@vito vito commented Jun 5, 2026 •

Copy link
Copy Markdown
Owner

What

A regex match used to be a bespoke MatchValue — its own Go struct with a Value impl and a special case MatchValue: branch in Select.Eval. That's overkill for what is really just a record of fields.

Now that Dang has a first-class Map type, a Match becomes a truly plain object (newMatch) whose members are materialized as ordinary fields. It flows through the normal case ValueScope: object path with no bespoke value type, no dispatch special-case, and no callable members.

  • Deleted the MatchValue struct and its Value/String/MarshalJSON impls, and the case MatchValue: dispatch in Select.Eval.
  • newMatch(re, src, indices) *Object binds string/start/end and the positional captures list as plain fields.
  • Named captures are now a named: Map[String] keyed by group name, replacing the old capture(name) callable. A named group that did not participate reads back as null, and an unknown name is an absent key (also null) — exactly the old capture(name) semantics, but as plain data you can index, iterate, and serialize.

Why MatchType stays

Select.Infer requires the receiver to be a named TypeScope to carry its field schemes, which structural record types aren't — so a match still needs a named type. MatchType is now just the type of a plain object rather than backing a bespoke runtime value. The Method(MatchType, …) registrations are kept as declaration-only members so the fields still type-check and appear in the auto-generated stdlib reference; their impls simply return the bound field.

API

let m = "555-1212".match(`(?P<area>\d{3})-(\d{4})`)
m.string            # "555-1212"
m.captures          # ["555", "1212"]   (positional, captures[0] is $1)
m.named["area"]     # "555"
m.named["nope"]     # null              (unknown name)

let opt = "bar".match(`(?P<pre>foo)?(?P<word>bar)`)
opt.named["pre"]    # null              (group did not match)
opt.named["word"]   # "bar"

Behavior changes vs. the previous MatchValue

  • capture(name) is removed in favor of named[name].
  • A match printed/interpolated directly renders as module Match (every real use goes through .string/.captures/.named).
  • Match JSON serialization is structural, e.g. {"captures":["","bar"],"end":3,"named":{"pre":null,"word":"bar"},"start":0,"string":"bar"}. No more bespoke MarshalJSON — and since there are no callable members, no special-casing is needed when marshaling the object.

Testing

go test ./pkg/dang/ ./tests/ passes (the language and error-golden suites, including tests/test_regexp.dang). The only failures in the full tree are the neovim/zed editor-highlight tests, which require editor submodules and a lua/neovim runtime not present in the dev sandbox.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

A regex match was a bespoke MatchValue type with its own Value impl and a
dedicated case in Select.Eval, which is overkill for what is really just a
record of fields. Now that Dang has a Map type, a Match can be an ordinary
plain object: newMatch binds string/start/end and the positional captures
list as fields, and named captures become a Map[String] keyed by group
name. A named group that did not participate reads back as null, and an
unknown name is an absent key, preserving the old capture(name) lookup
semantics without a callable field. This deletes MatchValue and the special
dispatch branch, so a Match now flows through normal object field access.

MatchType stays, since Select.Infer requires a named TypeScope receiver to
carry the field schemes; the Method(MatchType, ...) registrations remain as
declaration-only members for the type checker and the stdlib reference.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the refactor-match-plain-object branch from 8de3e2c to 2144654 Compare June 9, 2026 15:31
@vito vito changed the title refactor(regexp): make Match a plain object regexp: make Match a plain object with named captures Jun 9, 2026
@vito
vito merged commit ea18953 into main Jun 9, 2026
8 checks passed
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