dart-lang / dart-lang/language

Runtime-evaluated pattern matching function signatures.

Open
#3,368 4 comments 5 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

This shouldn't allow overloading, and should just be treated as syntactic sugar. Of course, functions that use patterns should be consistent and have a bottom-definition / should be exhaustive. Example below:

```dart
int fibonacci(int v case 0 || 1) => 1;
int fibonacci(int v) => fibonacci(v - 1) + fibonacci(v - 2);
```

This would be sugar for:

```dart
int fibonacci(int v) {
switch (v) {
case 0 || 1:
return 1;
default:
return fibonacci(v - 1) + fibonacci(v - 2);
}
}
```

An example of function of potentially unsafe overloading:

```dart
void process(Object _ case String v) => print("It is a string");
void process(Object _ case int v) => print("It is an int");
void process(Object _) => print("It's neither a string nor an int.");
```

This would mean that the functions must have the same signature, and is not fully fledged function overloading, but it's worth a discussion in my opinion.

Contributor guide

Open the contributing guide

Research direction

Start with the issue's two Dart examples and compare the proposed pattern syntax with its switch-based desugaring. Determine whether the language design should support this feature, including the same-signature and exhaustiveness constraints; done requires an agreed design direction rather than a localized code change.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.