Hacker News new | ask | show | jobs
by infogulch 3222 days ago
Would it be possible to make functions generic over coroutines/async?

One of the worst parts of async is there's an infectious duplication of pretty much all library code where there are separate sync/async versions of everything. See C# DoXxx() & DoXxxAsync() everywhere, where the only difference between the two implementations are an annotation and some keywords sprinkled throughout. If rust can do the same thing without doubling the code/api surface/documentation across all libraries that would allay the fears of many opponents.

2 comments

Haskell is doing this already with par and seq function to control parallel and lazy evaluation. I believe we should have something like this for Async
You can do DoXxx() using DoXxxAsync() by calling wait() on the future it returns. No duplicated code necessary.
It's nice that that'll work but it's... kinda gross. Can it call wait() automatically via some auto type conversion? Ideally in a synchronous context it would call wait(), and in an async context it would automatically annotate await (and you could always override by calling wait() manually).