有没有办法获取定义函数的文件的路径?
例如:
rootappdir
|- Foo.hs
|- Bar.hs
module Foo where
getThisDir :: IO Filepath
getThisDir = ...
prelude> getThisDir
absolute/path/to/rootappdir/Foo.hs
Run Code Online (Sandbox Code Playgroud)
如果可以使用更简单的函数:: Filepath,那就更好了.也许我们需要使用预处理器?
您需要使用Template Haskell来执行此操作.
{-# LANGUAGE TemplateHaskell #-}
import Data.Functor
import Language.Haskell.TH
import System.Directory
import System.FilePath
filePath :: String
filePath = $(do
dir <- runIO getCurrentDirectory
filename <- loc_filename <$> location
litE $ stringL $ dir </> filename)
Run Code Online (Sandbox Code Playgroud)