How can I get the position where error was called?

后端 未结 4 964
遇见更好的自我
遇见更好的自我 2021-01-04 11:52

I am looking for something to replace loch (and its preprocessor) since it doesn\'t compile with ghc 7.

Specifically, if error is called then I would l

4条回答
  •  半阙折子戏
    2021-01-04 12:08

    A quick-n-dirty workaround is the assert function from Control.Exception. However, it's a little clunkier than error.

    Warning: All asserts will be silently ignored if you compile with optimizations (ghc -O1, -O2, etc.).

    Example:

    import Control.Exception
    
    main = do
        print (42 + (assert True  17)) -- adds 42 and 17
        print (42 + (assert False 21)) -- crashes
    

    Output:

    59
    test.hs: /tmp/test.hs:5:18-23: Assertion failed
    

    Note the line number "5" in the output.

    You could use trace from Debug.Trace to add an error message:

        print (42 + (trace "omg error" $ assert False 21))
    

提交回复
热议问题