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

[PAGE ISSUE]: 'Asynchronous programming: futures, async, await': main as an asynchronous function? #5624

Closed
1 task
dtonhofer opened this issue Mar 5, 2024 · 3 comments
Labels
from.page-issue Reported in a reader-filed concern

Comments

@dtonhofer
Copy link

Page URL

https://dart.dev/codelabs/async-await/

Page source

https://github.com/dart-lang/site-www/tree/main/./src/content/codelabs/async-await.md

Describe the problem

At Working with futures: async and await

we see that function main() is rendered asynchronous:

Future<void> main() async { ··· }

but then the call is made:

print(await createOrderMessage());

Expected fix

The main() of the example should probably be createOrderMessage().

While the compiler (but maybe not the linter) is apparently happy with an asynchronous main returning a Future<void>, it seems also rather unusual.

Additional context

No response

I would like to fix this problem.

  • I will try and fix this problem on dart.dev.
@dtonhofer dtonhofer added the from.page-issue Reported in a reader-filed concern label Mar 5, 2024
@eernstg
Copy link
Member

eernstg commented Mar 5, 2024

Drive-by comment from a language perspective:

You can have situations where a top-level function named main is called from Dart code. (It may or may not be the same function as the one which is invoked from the runtime in order to run the whole program.) This implies that even though a function named main has a special role in a Dart program execution, it can also be just another top-level function.

Hence (and also just for consistency in a broader sense), there's nothing wrong in following all the normal rules about functions when writing the declaration of a top-level function named main.

It is true that most main functions have return type void, even when the body is async, but that's basically just breaking the normal rules about good style because "it doesn't matter because no Dart code will ever call this function".

With that in mind it's perfectly fine to use Future<void> as the return type of an async main. This implies that callers can await the computation performed during an execution of main, but the given future will be completed by an object which is of no interest (so the value of await main(...) should be ignored and discarded).

With that in mind there's nothing wrong with the following:

Future<void> main() async {
  ···
  print(await createOrderMessage());
  ···
}

The main() of the example should probably be createOrderMessage().

It is of course possible to rename and rewrite functions arbitrarily, but if we just delete the current main declaration and rename createOrderMessage to main then the returned Future<String> would be completed and the String would be ignored, which is surely not the intended behavior of the program. So I can't really see how a renaming operation like that would be useful.

@dtonhofer
Copy link
Author

Thank you! Got it!

Yes I was extremely confused when saying:

The main() of the example should probably be createOrderMessage().

In retrospect that makes no sense. The issues collapses to "what syntax (consistently) to use for main() async".

On the mentioned page, a few lines after

Future<void> main() async {

there is an example which just uses

void main() async {

So that line should be fixed I guess. I have opened an additional issue

#5626

that is hopefully clearer and will reference this one from there.

I reckon this issue can be closed without further delay.

@eernstg
Copy link
Member

eernstg commented Mar 5, 2024

Thank you! Got it!

Thanks for the kind words!

So that line should be fixed I guess.

Well, void main() async {...} is extremely common, so even though Future<void> main() async {...} would be nicer, I don't think that's going to get any traction. ;-)

@eernstg eernstg closed this as completed Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from.page-issue Reported in a reader-filed concern
Projects
None yet
Development

No branches or pull requests

2 participants