Template Haskell compile error when calling with different parameters

放肆的年华 提交于 2020-01-04 08:24:26

问题


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?

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.


回答1:


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))


来源:https://stackoverflow.com/questions/12144250/template-haskell-compile-error-when-calling-with-different-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!