簡體   English   中英

如何使用`reify`獲取函數的聲明?

[英]How to get the declaration of a function using `reify`?

函數reify允許我查找有關給定名稱的信息。 對於函數,返回值為VarI

data Info = ... |  VarI Name Type (Maybe Dec) Fixity  | ...

在這里,我可以檢查函數的類型,也想檢查其聲明。 但是,在VarI的第三個參數中,我始終看不到Nothing 有沒有辦法獲取函數的聲明?

VarI信息構造器上模板haskell文檔中

一個“值”變量(與類型變量相反,請參見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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM