In a lot of modern languages (such as Python), asynchronous functions (those that return coroutines) must be explicitly marked as such:
async function foo():
pass
Is there any specific reason for this, and if so are there any alternatives?
There are various solutions to avoid needing to mark functions as async.
Some languages work this way. All functions return promises in theory and can use your await syntax.
This is what Go does.
awaitSince a non-async function can't await, it makes sense to just automatically make functions with that syntax async. This is how generators work in Python, including yield makes them a generator.
Also if you don't have await or insert it implicitly there is no way to know.
Disadvantage is the return type changes depending on syntax. A stub method now returns the wrong type if it has no await.
If your language is statically typed and requires functions to have a return type specified, you could use that to detect if a function is async. Requires no special syntax.
Disadvantage is if your function returns a literal promise that wouldn't be possible. You could have separate types for automatic promises and manually created promises to resolve this issue, but then you have the whole complexity of converting between them and it might not be worth it.
await pass or something to that effect.
$\endgroup$
NotImplementedException immediately is still a type error because it won't return a generator without a yield. A await pass could give a potential syntax for async functions but still annoying to need to write code after the exception throw to make it work properly
$\endgroup$
await vs always having to add async? Seems like fully equivalent, you just swap what you have to add, but in either case you need to add something.
$\endgroup$
Marking asynchronous functions async is indeed unnecessary in many languages, as mousetail's answer explains. However, in C#, the async function marker keyword was needed for another reason: to to maintain backwards compatibility, in case an existing (non-async) method used await as a variable or method name. So await would only be treated as a keyword if it was in a method marked async. More details are given in this MSDN blog post.
I speculate that the async/await syntax was then simply copied into other languages like Python and JavaScript without too much thought, which is why it's present there, even when they may not have the same backwards-compatibility goals, or a better new design could've been used. But I can't be sure on that.
awaitoperator that doesn’t require this. $\endgroup$asyncness from the use ofawaits in the body? Though it's not like that isn't how generators work, it's still pretty clear why that would be undesirable... $\endgroup$await. $\endgroup$