Parameterization of partial functions by error type

I have a situation where most of the operations that I use are partial functions. I want the return type of functions to vary depending on the context that should determine the type of exception handling, as well as the type of error messages. My current solution is to define a function parameterized by error type, and then implement the operation as an open function using type classes. For example, the following implementation is an implementation of a chapter, suggesting a suitable implementation of Error and MonadError for Maybe in terms of unit type:

class (Error e, MonadError em) => Head me | m -> e where head :: [a] -> ma errorHead :: (Error e, MonadError em) => e -> [a] -> ma errorHead e [] = throwError e errorHead e (x : xs) = return x instance Head Maybe () where head = errorHead () instance Head (Either String) String where head = errorHead "error: empty list" 

The advantage of this implementation is that various errors can be selected depending on the context in which the head is called. The question I have is whether this can be done without defining a function using an auxiliary operation and public functions. If not, then the second question is whether this solution is optimal. In particular, is there a better way to achieve this behavior?

+4
source share
1 answer

The http://hackage.haskell.org/package/errors package has many utilities for moving between Maybe , Either , MaybeT and EitherT .

+1
source

Source: https://habr.com/ru/post/1444275/


All Articles