从同一个模块检索的注释 [英] Retrieving annotations from the same module
问题描述
假设我定义自己的注释类型:
Suppose I define my own annotation type:
{-# LANGUAGE DeriveDataTypeable #-}
module Def where
import Data.Data
data MyAnn = MyAnn Int deriving (Show, Typeable, Data)
和一些模板哈斯克尔函数来访问它:
and some Template Haskell function to access it:
module TH where
import Def
import Language.Haskell.TH.Syntax
myAnn :: Name -> Q Exp
myAnn name = do
[MyAnn x] <- reifyAnnotations (AnnLookupName name)
lift x
我现在想用这样的:
I would now like to use it like this:
{-# LANGUAGE TemplateHaskell #-}
module Client where
import Def
import TH
x :: ()
x = ()
{-# ANN x (MyAnn 42) #-}
y :: Int
y = $(myAnn 'x)
但这种失败,因为在是
定义的 myAnn
调用会从<$ C $空列表C> reifyAnnotations 。
它的工作原理,如果我分裂客户端
是这样的:
It works if I split Client
like this:
module Client1 where
import Def
x :: ()
x = ()
{-# ANN x (MyAnn 42) #-}
{-# LANGUAGE TemplateHaskell #-}
module Client2 where
import Client1
import TH
y :: Int
y = $(myAnn 'x)
有没有办法让类似单片客户端
模块的工作?
推荐答案
这似乎根据的的文档:
相应地,通过具体化
看到的类型的环境,包括所有的顶层声明到立即preceding声明组的结尾处,但没有更多。
Accordingly, the type environment seen by
reify
includes all the top-level declarations up to the end of the immediately preceding declaration group, but no more.
这是有效的解决方法您的问题是
通过包括一个空的顶级声明拼接,迫使单独的声明组的开始,即刚
An effective workaround for your problem is to force the start of a separate declaration group by including an empty top-level declaration splice, i.e. just
$(return [])
这篇关于从同一个模块检索的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!