Suppose I start with a function
fromJust Nothing = error "fromJust got Nothing!"
fromJust (Just x) = x
Then, I want to add source information via Template Haskell for better error messages. Let's imagine that I could add an extra parameter to the function
fromJust' loc Nothing = error $ "fromJust got Nothing at " ++ (loc_filename loc)
fromJust' loc (Just x) = x
and then have some fromJust
macro that I could use in source code like,
x = $fromJust $ Map.lookup k m
hack
I did manage to hack it, by using quasiquotes and lifting the string of the source filename. It seems that Loc
doesn't have a Lift instance. Is there a better way?
fromJustErr' l (Nothing) =
error $ printf "[internal] fromJust error\
\\n (in file %s)" l
fromJustErr' l (Just x) = x
fromJustErr = do
l <- location
let fn = loc_filename l
fnl :: Q Exp = TH.lift fn
[| fromJustErr' $fnl |]
Thanks!
(I know it's nicer to fmap
stuff via the Maybe
functor than use fromJust
, but I need to hack sometimes.)