繁体   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