It seems to me that an idiomatic way to validate input data in Haskell is via an applicative chain:
mkMyData :: a -> b -> c -> Maybe MyData
mkMyData x y z =
MyData
<$> validateA x
<*> validateB y
<*> validateC z
where the validation functions themselves return Maybe
values. To make my smart constructor mkMyData
more flexible, I would like it to return MonadThrow
. That is,
mkMyData :: MonadThrow m => a -> b -> c -> m MyData
Does this require each of the validation functions to return MonadThrow
instead of Maybe
? Or is there some way to convert the specific Maybe
result of each validation into the more general MonadThrow
without breaking up the applicative structure and greatly complicating the code?
Or maybe put differently? Is it worthwhile to strive for the more general MonadThrow
return type in basic library functions, at the expense of more complex, less idiomatic code?