假设我从一个函数开始
fromJust Nothing = error "fromJust got Nothing!"
fromJust (Just x) = x
然后,我想通过 Template Haskell 添加源信息以获得更好的错误消息。假设我可以为函数添加一个额外的参数
fromJust' loc Nothing = error $ "fromJust got Nothing at " ++ (loc_filename loc)
fromJust' loc (Just x) = x
然后有一些fromJust
我可以在源代码中使用的宏,例如,
x = $fromJust $ Map.lookup k m
黑客
我确实设法通过使用准引号并解除源文件名的字符串来破解它。似乎Loc
没有 Lift 实例。有没有更好的办法?
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 |]
谢谢!
(我知道fmap
通过Maybe
函子比使用更好fromJust
,但有时我需要破解。)