exceptionasynchronoushaskellcastingthrow

Example of using `linkOnly` from the async library


I found what I'd imagine to be a useful function in the illustrious async library that allows a thread to propagate an exception if and only if the exception passes a predicate - with a type of SomeException -> Bool.

This is the dilemma - SomeException doesn't implement Eq, so casting my particular exception type into SomeException would be fruitless. Likewise, I tried to use cast from Data.Typeable, but that didn't seem to work either.


Solution

  • cast from Data.Typeable will work, you just have to hold it right. For example:

    isIOError :: SomeException -> Bool
    isIOError (SomeException e) = isJust (cast e :: IOError)
    

    Of course you can write more exciting predicates that actually do some computation with your custom exception instance -- this one is just to demonstrate the base plan.