[英]How to get the declaration of a function using `reify`?
一個“值”變量(與類型變量相反,請參見
TyVarI
)。 如果RHS對編譯器不可用,則Maybe Dec
字段Just
包含定義變量的聲明-包括聲明的RHS-否則為Nothing
。 目前,此值始終為Nothing
:由於缺乏興趣,尚未執行返回RHS的操作。
查看github上的ghc源鏡像 , 字符串VarI
僅出現兩次 ,並且都在實現reifyThing
函數的編譯器 reifyThing
:
reifyThing :: TcTyThing -> TcM TH.Info
-- The only reason this is monadic is for error reporting,
-- which in turn is mainly for the case when TH can't express
-- some random GHC extension
reifyThing (AGlobal (AnId id))
= do { ty <- reifyType (idType id)
; fix <- reifyFixity (idName id)
; let v = reifyName id
; case idDetails id of
ClassOpId cls -> return (TH.ClassOpI v ty (reifyName cls) fix)
_ -> return (TH.VarI v ty Nothing fix)
}
reifyThing (AGlobal (ATyCon tc)) = reifyTyCon tc
reifyThing (AGlobal (ADataCon dc))
= do { let name = dataConName dc
; ty <- reifyType (idType (dataConWrapId dc))
; fix <- reifyFixity name
; return (TH.DataConI (reifyName name) ty
(reifyName (dataConOrigTyCon dc)) fix)
}
reifyThing (ATcId {tct_id = id})
= do { ty1 <- zonkTcType (idType id) -- Make use of all the info we have, even
-- though it may be incomplete
; ty2 <- reifyType ty1
; fix <- reifyFixity (idName id)
; return (TH.VarI (reifyName id) ty2 Nothing fix) }
reifyThing (ATyVar tv tv1)
= do { ty1 <- zonkTcTyVar tv1
; ty2 <- reifyType ty1
; return (TH.TyVarI (reifyName tv) ty2) }
reifyThing thing = pprPanic "reifyThing" (pprTcTyThingCategory thing)
就像模板haskell docs所說的那樣,用於該字段的值始終為Nothing
。
挖掘死神, 此代碼在2003年添加,看起來像是對reify系統的重寫。 因此,它似乎是在得到它的工作,因為它已經超過10年的那場一直有值一點興趣Nothing
。 因此,我猜測如果您需要該功能,則必須自己實現(或向ghc開發郵件列表提出一個很好的用例,以鼓勵其他人使用它)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.