dart-lang / dart-lang/language

Make it easy to use type B in place of type A when there is an obvious/explicit conversion from B -> A

Open
#107 3 comments 12 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Every place in `pkg:http` with a `url` parameter types it as `dynamic`.

```dart
Future get(url, {Map headers}) => ...

void doWork() async {
await get('http://example.com');
await get(Uri.parse('http://example.com'));
await get(42); // statically fine, but causes a runtime error
}
```

This is to support a common use case: devs often want to pass either `Uri`
*or* `String` to such methods. In the end, all `String` values are "upgraded"
to `Uri` before use. To support the desired flexibility, the user risks
runtime errors if something other than `String` or `Uri` are provided.

Flutter avoids this issue, by being explicit about types everywhere.

```dart
// Flutter
void giveMeABorder(BorderRadiusGeometry value) {}

void doWork() {
giveMeABorder(const BorderRadius.all(
Radius.circular(18),
));

// User would like to write this, but...
giveMeABorder(18); // static error
}
```

The existing request(s) for union types – https://github.com/dart-lang/sdk/issues/4938 and https://github.com/dart-lang/language/issues/83 – could be an option here, but it would require updating all parameters and setters to specify the supported types.

It'd be nice to be able to define explicit conversions so one could easily pass `String` to properties/params requiring `Uri`, etc.

Contributor guide

Open the contributing guide

Research direction

Start with the `pkg:http` examples in the issue and review the linked union-type proposals at dart-lang/sdk#4938 and dart-lang/language#83. Define the language behavior for explicit conversions, including which `String` and `Uri` uses become statically valid and how unsupported values are rejected.

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.