Maintainer | Brandon Chinn <brandon@leapyear.io> |
---|---|
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
This module defines utilites for testing Template Haskell code.
Error recovery
Unfortunately, there is no built-in way to get an error message of a Template Haskell
computation, since recover
throws away the error message. If
recover
was defined differently, we could maybe do:
recover' :: (String -> Q a) -> Q a -> Q a spliceFail :: Q Exp spliceFail = fail "This splice fails" spliceInt :: Q Exp spliceInt = [| 1 |] test1 :: Either String Int test1 = $(recover' (pure . Left) $ Right <$> spliceFail) -- generates `Left "This splice fails"` test2 :: Either String Int test2 = $(recover' (pure . Left) $ Right <$> spliceInt) -- generates `Right 1`
But for now, we'll have to use tryQ
:
test1 :: Either String Int test1 = $(tryQ spliceFail) -- generates `Left "This splice fails"` test2 :: Either String Int test2 = $(tryQ spliceInt) -- generates `Right 1`
tryQ' :: Q a -> Q (Either String a) Source #
Run the given Template Haskell computation, returning either an error message or the final result.