TemplateHaskell and IO

我的未来我决定 提交于 2019-12-09 12:24:22

问题


Is there any proper way to make TH's functions safe if they use side effects? Say, I want to have a function that calls git in compile time and generates a version string:

{-# LANGUAGE TemplateHaskell #-}

module Qq where

import System.Process
import Language.Haskell.TH

version =  $( [| (readProcess "git" ["rev-parse", "HEAD"] "")  |] )

the type of version is IO String. But version is completely free of side effects in runtime, it has side effects only in compile time. Is there any way to make it pure in runtime without using unsafePerformIO ?


回答1:


First: normally, the runtime type of the generated code is independent of the compile-time type of the Template Haskell subexpressions, so the runtime type doesn't have to be in IO.

Now, to run this command without using unsafePerformIO, use runIO. You will then have to construct the Expr yourself, without using [| |] (this also solves the type problem).

Actually, if you use [| |] to insert an IO computation, I think it will only insert the computation, not run it, anyway. But that's an irrelevant aside, because regardless of what it does, that's not the right way to do what you want to do.



来源:https://stackoverflow.com/questions/5713418/templatehaskell-and-io

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