我正在编写一个模板haskell库供我自己使用,它可以生成函数。我想要做的是将这些生成的函数传递给酸状态的makeAcidic TH函数。
但是,在我的Q上下文中简单传递生成的名称的结果会导致此错误:
不在范围内
如何将新创建的函数放在makeAcidic工作的范围内?
我的代码:
gen_render :: Name -> Q [Dec]
gen_render typName =
do (TyConI d) <- reify typName -- Get all the information on the type
(type_name, vars, nars, constructors) <- typeInfo (return d) -- extract name and constructors
x <- mapM func_def constructors
let funcs = concat x
let setterNames = [setter | (setter, _) <- funcs]
acidic <- makeAcidic typName setterNames
return $ [newFuncs | (_, newFuncs) <- funcs] ++ acidic
where func_def data_con@(con_name, compontents)
= do
x <- mapM genFieldSetandGet compontents
return $ concat x
genFieldSetandGet (Just fieldName, fieldType) = do
uVar <- runQ $ varE $ mkName "u"
ixInsert <- appsE $ [[| IxSet.insert |], (varE $ mkName "u"), parensE $ appsE [(varE fieldName), (varE $ mkName "db")]]
{-instanceD (cxt [])
(conT (mkName "DatabaseAccess") `appT` (return fieldType) `appT` conT typName)-}
let setterName = (mkName $ "updateDb" ++ (capitalize $ nameBase fieldName))
func <- funD setterName $
[clause [(varP $ mkName "u"), (varP $ mkName "db")] (normalB $
[| modify |] `appE` [| IxSet.insert |] `appE` (varE $ mkName "u") ) []]
return [(setterName, func)]
答案 0 :(得分:0)
您必须确保在makeAcidic拼接之前运行拼接。