问题描述
为什么以下编译失败(在GHC 7.4.2上)?
Why does the following fail to compile (on GHC 7.4.2)?
{-# LANGUAGE TemplateHaskell #-}
f1 = $([| id |])
main = print $ (f1 (42 :: Int), f1 (42 :: Integer))
请注意,以下代码可以编译:
Note that the following compiles fine:
{-# LANGUAGE TemplateHaskell #-}
f1 = id -- Don't use template Haskell here.
main = print $ (f1 (42 :: Int), f1 (42 :: Integer))
是否有可用于编译前语言的语言扩展?
Is there a language extension I can use to make the former compile?
我知道模板Haskell在这个例子中似乎很愚蠢,但它是一个更复杂问题的简化版本,它需要Template Haskell处理任意大小的元组。
I know the Template Haskell seems silly in this example, but it's a simplified version of a more complex problem, which requires Template Haskell to process arbitrary sized tuples.
推荐答案
显然 f1
被分配了类型整数 - >整数
而不是更一般的 a - >出于某种原因,
。
Apparently f1
is assigned the type Integer -> Integer
instead of the more general a -> a
for some reason. Adding an explicit type signature makes your example compile fine for me:
{-# LANGUAGE TemplateHaskell #-}
f1 :: a -> a
f1 = $([| id |])
main = print $ (f1 (42 :: Int), f1 (42 :: Integer))
这篇关于使用不同参数调用时,模板Haskell编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!