Retrieving alerts from Haskell templates

I know that I can cause a compile-time error by causing fail from splicing, but is it possible to generate a warning? In particular, I would like this message to be turned into a compilation error with -Werror .

Essentially, I'm trying to do the following:

 todo :: Q Exp todo = do -- emit warning somehow loc <- location let message = ... -- generate message based on loc [| error $(litE (stringL message)) |] 

The idea is to use this instead of undefined during coding, but make sure that it does not sneak into production code by compiling with -Werror .

 myFunc x | isSimpleCase x = 42 | otherwise = $todo 
+6
source share
3 answers

Turns off the function that I was was the Template Haskell report function. His type signature was in the documentation, but I had to read the source code to find out what he did. The TH documentation may surely use some enhancements.

Anyway, my todo placeholder now works fine, and I put something in Hackage if anyone is interested.

+5
source

I don't think this is possibly naive from TH, but it is a really cool idea.

One way to implement it is to attach a warning and debug output or to the GHC-API .

eg. qualify for the role of GHC,

 import Panic main = sorry "help!" 

produces

 $ ./AA: A: sorry! (unimplemented feature or known bug) (GHC version 7.0.2 for x86_64-unknown-linux): help! 

Building GHC warnings should work the same way, checking if -Werror installed, and you can clear the API to be quite useful.

+2
source

To emit warnings from Haskell splines, you can use reportWarning :: String -> Q () .

It already includes the location (row and column). You can implement your todo function simply:

 todo :: Q Exp todo = do reportWarning "TODO" [| undefined |] 

Additional Information

@Hammar's answer points to the report function. It is deprecated after GHC 7.6 (2012) and may be removed from the API in the near future. (However, report is still available in GHC 7.10 as well as in the GHC master branch since 2015.)

Use reportError to report an error and continue computing Q (ultimately, failed compilation).

Use fail to stop with an error (GHC ≤ 7.10). Which may not apply to GHC 8.0 .

0
source

Source: https://habr.com/ru/post/886597/


All Articles