Is there a way to get the path of the file where the function is defined?
For example:
rootappdir
|- Foo.hs
|- Bar.hs
module Foo where
getThisDir :: IO Filepath
getThisDir = ...
prelude> getThisDir
absolute/path/to/rootappdir/Foo.hs
If it is possible with an even simpler function :: Filepath, that's even better. Maybe we would need to use the preprocessor?
I suppose you can't get this information in run-time. But you can get it in compile-time through Template Haskell using function Language.Haskell.TH.location
or qLocation
.
If you need logging functionality you can use package monad-logger. You can find an example of using qLocation
there.
You need to use Template Haskell to do this.
{-# 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With