dart-lang / dart-lang/language

[Pattern-matching] Allow :name syntax in switch statements

Open
#3,092 10 comments 4 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

There's a shorthand syntax in object matching
![image](https://github.com/dart-lang/language/assets/12999702/4099dcb1-9b87-4906-bf1a-f13e571f802b)

I tried to use it in switch statements and expected it will work.
My expectation: `Data()` will be destructured and its `name` parameter will be binded to a corresponding new `name` variable automatically.

but it fails with following errors:

It works only if I use an explicit syntax

But it looks excessive.

It also ain't working in switch expressions:

## Proposal: ##

Allow using the `:name` syntax in object matching in switch statements and switch expressions

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the object-matching examples and the reported failures in switch statements and switch expressions. Compare the proposed :name shorthand with the explicit syntax shown in the issue, then determine the language-specification changes needed for destructuring and binding. Done means the shorthand has defined behavior and works consistently in both switch forms.

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
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.