zhensydow/opencl — Issue #12: Error-handling is suboptimal
Author: ehird
Created: 2011-11-18 21:00:26+00:00
State: open

I'm interesting in using this library, but the IMHO suboptimal and non-idiomatic error handling style is putting me off a bit.

It would be much nicer if, instead of `IO (Either CLError a)`, every OpenCL action was `IO a`, and an [exception](http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/Control-Exception.html) type was used, such as:

newtype CLException = CLException { clError :: CLError } deriving (Show, Typeable)
instance Exception CLException

This allows handling errors just as simply as with the current API; this code:

do result <- clAction
case result of
Left err -> handle error
Right a -> ...

becomes

do a <- clAction `catch` \(CLException err) ->
handle error
...

and the simpler case, in which errors are not handled at this point (bubbling up to a higher handler or just making the program fail with an error message if there is no reasonable recovery path) becomes a lot simpler; the code:

either (fail . show) id <$> clAction

(à la your [myTry](https://github.com/zhensydow/opencl/blob/master/examples/example01.hs) -- but it's bad practice to use `error e` as a monadic value; `fail e` or `return (error e)` are better, but the latter only triggers when you force the value returned)

becomes simply:

clAction

And if the original error-returning behaviour is desired, it can be recreated as simply as this:

try clAction

(with [Control.Exception.try](http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/Control-Exception.html#v:try))

Thanks for taking the time to consider this request! :)