managarm / frigg

Lightweight C++ utilities and algorithms for system programming
MIT License
56 stars 20 forks source link

expected: map([...] (...) -> expected<...> { ... }) produces expected<expected> #41

Open ArsenArsen opened 2 years ago

ArsenArsen commented 2 years ago

While this behavior is probably fine, it would be desirable to be able to chain expecteds for monadic operations.

For an example of such see Haskell's Either

-- Module      :  Data.Either
-- Copyright   :  (c) The University of Glasgow 2001
-- License     :  BSD-style (see the file libraries/base/LICENSE)

-- | @since 4.4.0.0
instance Monad (Either e) where
    Left  l >>= _ = Left l
    Right r >>= k = k r

In essence, this means that >>= propagates error and does not evaluate k (type: a -> Either e b) unless the Either is a Right (success case)

EDIT: similarly, there's no way to implicitly convert to a different expected result of same error (one would have to do if (!e) { {co_}return e.error(); })

avdgrinten commented 2 years ago

This functionality is useful, of course, but it'd rather have it in a separate function (other than map()) such that the function that is passed in can always be assumed to return a frg::expected.

ArsenArsen commented 2 years ago

Agreed, that's what I meant by "While this behavior is probably fine, it would be desirable to be able to chain expecteds for monadic operations". A sensible name might be then or chain