テストのassertationやエラーログなどで使う。
GHC.Stackを使うとstackを取得できる。
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ImplicitParams #-}
import Data.Text (Text)
import GHC.Stack
main = do
putStrLn "before error"
printStack - line 9
putStrLn "after error"
printStack = do
putStrLn . show . head . reverse . getCallStack $ ?loc -- line 14
Output
before error
("?loc",SrcLoc {srcLocPackage = "main", srcLocModule = "Main", srcLocFile = "app/Main.hs", srcLocStartLine = 14, srcLocStartCol = 55, srcLocEndLine = 14, srcLocEndCol\
= 59})
after error
ここで、printStackに制約を付け足すと、ここでのstackは無視されるようになる。
printStack :: (?loc :: CallStack) => IO ()
Output
before error
("printStack",SrcLoc {srcLocPackage = "main", srcLocModule = "Main", srcLocFile = "app/Main.hs", srcLocStartLine = 9, srcLocStartCol = 5, srcLocEndLine = 9, srcLocEnd\
Col = 15})
after error