dart-lang / dart-lang/language

Functions downcast

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

Description

Consider the following definitions:

```dart
void foo(int a, String b) {}

typedef MyCallback = void Function(int a, String b, double c);
```

Currently, we cannot assign the function `foo` to `MyCallback` and instead have to write such closure:

```dart
MyCallback cb = (int a, String b, _) => foo(a, b);
```

This proposal is to allow such assignment:

```dart
MyCallback cb = foo;
```

### Reasoning

While the current behavior is technically correct, it makes adding parameters to a callback a breaking change.
And at the same time, it is common to not care about some parameters passed to a callback (hence why we have `_`).

There are many examples where such a feature may be useful.

For example, `Iterable.map`/`Iterable.forEach` may want to pass the index of the item to the callback as a second parameter:

```dart
[].map((value, index) => something);
```

Another example is the optional `child` argument on the `Builder` pattern of Flutter widgets.
Some widgets like `AnimatedBuilder` or `ValueListenableBuilder` takes an optional `child` for performance optimizations like so:

```dart
ValueListenableBuilder(
valueListenable: listenable,
// `child` passed to the function is the value pased to ValueListenableBuilder constructor
builder: (context, value, child) => something,
child: // cached widget
);
```

But some widgets such as `StreamBuilder` currently do not offer such `child` argument – which makes them inconsistent.
Ideally, these widgets should take an optional `child`, but again that is a relatively significant breaking change.

With this proposal, the changes described above would **not** be a breaking change any more.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.