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

Choose the type argument for the future returned from an invocation of an async function #33213

Closed
eernstg opened this issue May 23, 2018 · 5 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). area-specification (deprecated) Deprecated: use area-language and a language- label.

Comments

@eernstg
Copy link
Member

eernstg commented May 23, 2018

When an async function is invoked, a fresh instance of Future<T> for some T is returned at some point, and there is no user-written code which creates that future so the choice of T is not made by a developer. The specification currently leaves it unspecified which value to use for T:

If f is marked async (9), then a fresh instance (10.6.1) o implementing
the built-in class Future is associated with the invocation. Then the body
of f is executed until it either suspends or completes, at which point o is
returned.

This issue serves to recommend that we specify this type argument explicitly.

To get started we may consider a couple of loose ideas: Future<T> f() async {...} should return an instance of Future<T>. void f() async {...} should return an instance of Future<void>. FutureOr<T> f() async {...} should probably return an instance of Future<T>. Etc.

@eernstg eernstg added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). area-specification (deprecated) Deprecated: use area-language and a language- label. labels May 23, 2018
@eernstg eernstg self-assigned this May 23, 2018
@eernstg
Copy link
Member Author

eernstg commented May 23, 2018

@lrhn, @leafpetersen do you agree that we should extend the specification to make an explicit choice in this area? I suppose it will fit as an update of dartLangSpec.tex directly, with no intervening feature specification.

@leafpetersen
Copy link
Member

Yes, we should specify this. I think the right answer is that the returned value should be a Future<flatten(returnType)>.

@eernstg
Copy link
Member Author

eernstg commented May 24, 2018

Right, that would be as follows (declared return type --> dynamic type of returned Future):

void --> Future<void>
dynamic --> Future<dynamic>
Object --> Future<Object>
Future<T> --> Future<T>, for all T
FutureOr<T> --> Future<T>, for all T

The first three may seem slightly accidental, but they also seem "natural" (and, of course, as a run-time type they would all be Future<Object> except for printing).

@eernstg
Copy link
Member Author

eernstg commented May 24, 2018

I've created a CL updating dartLangSpec.tex to say this (including, of course, similar rules for sync* and async*).

The specification doesn't actually prevent an implementation from returning an object whose clausal supertype at Future/Stream/Iterable has an actual type argument which is a proper subtype of the specified return type (that is "the returned instance can be too special"). For instance, with

Future<String> returnsString() async {
  return null;
}

it would be acceptable for an invocation of returnsString to return a Future<Null>. I expect that to be a relatively harmless kind of imprecision, considering it to be a matter of "quality of service" that the implementation won't needlessly set up the future completion action for failure, and when it works it won't hurt, as far as I can see.

dart-bot pushed a commit that referenced this issue Jun 14, 2018
Addressing #33213.

Change-Id: I383d496c46fa0d1bf9313f315ad767eb21577eb6
Reviewed-on: https://dart-review.googlesource.com/56487
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
@eernstg
Copy link
Member Author

eernstg commented Jun 14, 2018

Closing: Said CL has now been landed as commit 8dca855.

@eernstg eernstg closed this as completed Jun 14, 2018
lrhn pushed a commit to dart-lang/language that referenced this issue Aug 22, 2018
Addressing dart-lang/sdk#33213.

Change-Id: I383d496c46fa0d1bf9313f315ad767eb21577eb6
Reviewed-on: https://dart-review.googlesource.com/56487
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
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). area-specification (deprecated) Deprecated: use area-language and a language- label.
Projects
None yet
Development

No branches or pull requests

2 participants