dart-lang / dart-lang/language

Extensible pattern-matching

Open
#1,047 22 comments 97 reactions 0 assignees View on GitHub
patterns request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

_This issue was originally filed by sammcca...@google.com_

---

It would be nice to match a value against patterns with a terse syntax.

This is similar to 'switch' (and could possibly share the same syntax) but instead of exact-match, the semantics would be determined by the pattern used.

When the pattern is a RegExp, it would match strings that conform to it.
A Type would match instances of that type.
User classes should be able to implement their own pattern behaviour.
Any value without special pattern behaviour would match using ==

for example:
```
switch (value) {
match Exception: throw value;
match const RegExp('\s+') return 'whitespace';
match 42: return 'the answer';
default: return 'unrecognized';
}
```

There was some discussion here: https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/a3e75c24c6dd4f03/

A pattern might also be able provide values which can be bound to names (e.g. the Match object corresponding to a regexp match).

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the proposed syntax and semantics in this issue, then read the linked Google Groups discussion for prior design context. A complete outcome would require an agreed pattern-matching model covering regular expressions, types, user-defined behavior, equality fallback, and possible bindings; no implementation files or tests are identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.