Skip to content
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

Future<void>/FutureOr<void> require returns? #32443

Closed
matanlurey opened this issue Mar 7, 2018 · 6 comments
Closed

Future<void>/FutureOr<void> require returns? #32443

matanlurey opened this issue Mar 7, 2018 · 6 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@matanlurey
Copy link
Contributor

The following triggers an analysis warning in 2.0.0-dev.32.0:

import 'dart:async';

void returnNothing() {
  return; // OK
}

Future<void> returnNothingInTheFuture() async {
  return; // ERROR: Expecting return.
}

FutureOr<void> returnNothingMaybeInTheFuture() async {
  return; // ERROR: Expecting return.
}

I can silence it with return null; ... but can't that be automatic?

@eernstg
Copy link
Member

eernstg commented Mar 7, 2018

Agreed, when return expressionOfTypeT; is allowed in a function Future<T> foo() async .., it is very natural to allow return; in a function Future<void> foo() async ...

We would need a special exception for this, of course, and there may be other cases. We have had some discussions in #32177, and I've added a reference to this issue there.

@lrhn
Copy link
Member

lrhn commented Mar 7, 2018

These are tricky cases, and I don't think we agree entirely on what to say about it.

My personal guiding rules are that:

  • return; and return voidExpression; must be valid in the same cases (if we allow the latter at all, which we have historically done) ...
  • ... and then return; is really just equivalent to return null as void; (if we allow ... as void) ...
  • ... and the typing rules for return voidExpression; says that it's allowed in an async function if Future<void> is assignable to the return type of the function.

The third entry is the general rule for return e;. A return e; is type-correct if Future<flatten(typeOf(e))> is assignable to the return type of the surrounding function. We know that the actual return value is a Future<flatten(returnType)>, so in the async cases here, the actual return object is a Future<void>, and the Future<flatten(typeOf(e))> thing is really a way of saying that the value of e can be used to complete the actually returned future.

So, I would want the second and third examples to be valid, because they would be for return voidFunction();.

We don't currently have a Dart 2 compatible specification for the return; case, so the law is still open.

@leafpetersen leafpetersen added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Mar 8, 2018
@leafpetersen leafpetersen self-assigned this Mar 9, 2018
@Hixie
Copy link
Contributor

Hixie commented Mar 15, 2018

The current situation is clearly just wrong:

import 'dart:async';

void foo1() {
  return;
}

Null foo2() {
  return;
}

Future<void> foo3() async {
  return; // warning • Missing return value after 'return' at test.dart:12:3 • return_without_value                                                                                                                                                            
}

Future<Null> foo4() async {
  return;
}

void main() { }

@imampri102
Copy link

I think you can use like this:

import 'dart:async';

void foo1() {
  return;
}

Null foo2() {
  return;
}

Future<void> foo3() async {
  return Future<void>(() {});                                                                                                                                                      
}

Future<Null> foo4() async {
  return Future<Null>(() {});                                                                                                                                                      
}

@eernstg
Copy link
Member

eernstg commented Sep 23, 2020

With recent updates to the rules about returning (esp. dart-lang/language#941), there are no errors in the following:

import 'dart:async';

void foo1() {
  return;
}

Null foo2() {
  return;
}

Future<void> foo3() async {
  return;
}

Future<Null> foo4() async {
  return;
}

@wechulimaven
Copy link

am getting this error on slider OnChange() function
The return type 'Future' isn't a 'void', as required by the closure's context.dart return_of_invalid_type_from_closure

writting custom function that returns null this the error am getting
This expression has a type of 'void' so its value can't be used.
Flutter 2.5.0 stable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

7 participants