[Haskell-cafe] Yet another monad transformer or silly usage of
Either?
Job Vranish
job.vranish at gmail.com
Sun Jul 25 12:44:12 EDT 2010
Yeah, ErrorT should do what you want (EitherT is probably essentially the
same thing)
login would have the type:
login :: String -> String -> ErrorT DServError IO LoginResponse
and you would use it like this:
result <- runErrorT $ authenticatedReq
You can use runErrorT, or catch when you want to process a possible error.
result would have the type Either DServError whatever. This would leave out
the Result type, but if you really want to, you can add it with the
appropriate lifting.
Hope that helps,
- Job
2010/7/25 Eugeny N Dzhurinsky <bofh at redwerk.com>
> Hello, everybody!
>
> I am trying to develop some sort of library, which supposed to sign into a
> WEB
> service, then perform some requests with it.
>
> Initially I designed methods in the following way
>
> data DServError = InvalidCredentials | InvalidRequest | ...
>
> newtype Result a = Result { getOpResult :: Either DServError a }
>
> data DSession = Session { ... }
>
> data DLoginResponse = LoginResponse { currentSession :: DSession, ... }
>
> login :: String -> String -> IO ( Result LoginResponse )
>
> servRequest1 :: DSession -> ParamType1 -> ParamType2 -> ... -> IO ( Result
> DServResponse )
>
>
> Now I want to be able of doing something like
>
> authenticatedReq = do
> loginResponse <- login "username" "password"
> let session = currentSession loginResponse
> servRequest1 session ... ... ...
> servRequest2 session ... ... ...
> ...
>
> so if login succeeds - I will be able to extract Right data from the Either
> response ( with
> explicit or implicit usage of getOpResult), if any of operations within
> "do"
> block will fail with DServError - then an error should be reported.
>
> I think the solution for this may be using Control.Exception and it's
> try/catch? Or may be there's some trick available for Either?
>
> I looked at EitherT, and it seems that I have to wrap every invocation into
> EitherT and then chain them with >>/>>=
>
> --
> Eugene Dzhurinsky
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100725/ca6074fc/attachment.html
More information about the Haskell-Cafe
mailing list