模板Haskell类型引用问题 [英] Template Haskell type quoting problems
问题描述
TemplateHaskell引用两个引号(''
)作为获取类型名称的方式:
The TemplateHaskell quoting documents two quotes (''
) as the way to get the Name of a type:
> ''String
GHC.Base.String
这适用于此类型(名称) 。但是,我无法找到一种使其适用于 Maybe String
:
This works fine for this type (name). However, I can't find a way to make it work nice for e.g. Maybe String
:
> ''Maybe String -- interprets String as a data constructor
> ''Maybe ''String -- wants to apply ''String to the Name type
我知道我可以通过使用 [t |可能是String |]
,但这是在Q monad中,并且需要更改类型,并且我认为不是在相应时刻进行类型检查,仅当拼接时。
I know I can workaround via using [t| Maybe String |]
, but this is then in the Q monad, and requires type changes, and I think is not type-checked at the respective moment, only when spliced in.
我也可以通过首先定义一个类型别名, type MaybeString = Maybe String
,然后使用' 'MaybeString
,但这也很麻烦。
I can also work around by first defining a type alias, type MaybeString = Maybe String
, and then using ''MaybeString
, but this is also cumbersome.
通过''直接获取我想要的任何方法 quotation?
Any way to directly get what I want simply via the ''
quotation?
推荐答案
也许
是一个名字,也许字符串
不是。因此,在引用该名称之前,必须通过定义类型别名来赋予类型名称并不令人感到意外。
''
is used to quote names, not types. Maybe
is a name, Maybe String
is not. It is therefore not too surprising that you have to give your type a name by defining a type alias, before you can quote that name.
[ T | |]
,引号类型。注意这里的区别。
[t| |]
on the other hand, quotes types. Note the difference here.
Prelude> :t ''String
''String :: Language.Haskell.TH.Syntax.Name
Prelude> :t [t| String |]
[t| String |]
:: Language.Haskell.TH.Syntax.Q Language.Haskell.TH.Syntax.Type
所以我担心你不能用'
来做你想做的事情。
So I'm afraid you cannot use ''
for what you're trying to do.
这篇关于模板Haskell类型引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!