Skip to content

Avoid single void argument #1590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ykmnkmi opened this issue Apr 16, 2021 · 3 comments
Open

Avoid single void argument #1590

ykmnkmi opened this issue Apr 16, 2021 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@ykmnkmi
Copy link

ykmnkmi commented Apr 16, 2021

A feature that allows you to pass a function with no arguments to a function that accepts function with one void argument:

void doSomething() {}

Stream<void> stream;
Future<void> future;
stream.listen(doSomething);
future.then(doSomething);
doSomething();

instead

void doSomething([void _]) {}
@ykmnkmi ykmnkmi added the feature Proposed language feature that solves one or more problems label Apr 16, 2021
@lrhn
Copy link
Member

lrhn commented Apr 18, 2021

For this to work, we'd likely have to wrap the function going in so that it has the correct type. That is, assigning a function f of type X Function() to a context expecting an X Function(void) (or X Function([void])) will automatically wrap it as (void _) => f();.
That's type coercion, something Dart has so far shied away from. It has a number of consequences, the main one is that it introduces a new function with a different identity. If you do Set<void Function(void)> s = {}; s.add(f); s.remove(f);, there is a real risk that the two functions won't be identical. They might be equal, but that's something we have to work for.

Would the feature only work for void parameters? Would it work for more than one void parameter? Would it work if there are more non-void parameters, and only trailing (or named) void parameters are missing?

I'm not sure that complexity outweighs just doing stream.listen((_) => something());.

@mateusfccp
Copy link
Contributor

If you feel like, you can make an extension method, like:

import 'dart:async';

void main() {
  final s = StreamController<void>();
  s.stream.listen(doSomething.v);
}

void doSomething() {}

extension FunctionVoider<T> on T Function() {
  T Function(void) get v => (_) => this();
}

@ykmnkmi
Copy link
Author

ykmnkmi commented Oct 5, 2021

Due to strong typing, I have to write a type for _, without void type Missing parameter type for '_' error thrown:

request.response.close().then((void _) { /* */ });

Can we use void like in C without _:

request.response.close().then((void) { /* */ });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants