Subject: Re: [boost] Interest in an 'either' library?
From: Larry Evans (cppljevans_at_[hidden])
Date: 2013-06-24 17:52:12
On 06/24/13 13:53, Pierre Talbot wrote:
> In the Expected proposal, there are some methods related to this idea.
> Actually, the "then" method can be applied on an expected and
> immediately returns the expected if it contains an error. Furthermore,
> you can chain function calls until one of these fail.
This sounds similar to the Error Monad described here:
The example given there is one where the errors produced are caused
by dividing by 0. This can happen in 2 places and the 1st to fail
produces an error message. The example code from the above is:
divSum3 :: Float -> Float -> Float ->
Either String Float
divSum3 x y z = do
xdy <- myDiv3 x y
xdz <- myDiv3 x z
return (xdy + xdz)
in which xdy or xdz can be zero which causes the error.
IIUC, the first one which causes the error causes the
propagation of that error to the return. IOW, the above
example can be extended to any number of divisors:
-> Either String Float
divSum3 x d1 d2 ... dn = do
xd1 <- myDiv3 x d1
xd2 <- myDiv3 x d2
xdn <- myDiv3 x dn
return (xd1 + xd2 + ... + xdn)
Why not use something like haskell's Error Monad to do something
similar? Several years ago, I remember a Georgia Tech proposal
to do that. IIUC, they also had some sort of c++ monad template.