As I said in https://lists.isocpp.org/std-proposals/2020/09/1811.php, this already exists in C++20 in the form of coroutines... you just need to opt in support for optional<T> in the only sensible way and then you can write:

std::optional<std::string> FindUsersCity(bool non_default) {
    ConstactsSeriver cs = co_await GetOrOpenContatsServerConnection();
    UserId uid = co_await cs.GetUserId();
    GeoServer gs = co_await GetOrOpenGeoServerConnection();
    Location uloc = co_await gw.GetLocationId(uid);
    return uloc.GetCityName();
}

co_await here is effectively syntax sugar for and_then() (in the same way in Haskell do-notation is syntax sugar for bind). It's just that the continuation gets written as the next statement in the function rather than the body of a lambda that gets passed as an argument. 

That is really nice, but can you elaborate more?
Am I right that the trick is that expression of co_await can return an awaitable, await_ready() of which can return:
  • either false (in which case we effectively return from FindUsersCity and it corresponds to some error),
  • or it can return true (in which case FindUsersCity continues, and this corresponds to execution without errors)?

--
Dmitry
Sent from gmail