branch | status |
---|---|
master |
|
develop |
Assertion with condition string representation in Haskell
Add this library to your stack.yaml
like the following if you use Stack.
...
extra-deps:
- git: https://github.com/nwtgck/assertion-haskell.git
commit: 4340a96555606d590d54669b9ae992a5b8c9a10d
...
Then, add assertion
to your package.yaml
.
...
library:
dependencies:
- assertion
...
{-# LANGUAGE QuasiQuotes #-}
import Assertion
main :: IO ()
main = do
let a = "hello"
let b = 10
let !x = [assert|a == "hello" && b > 9999999|]
putStrLn "End!"
ExampleMain.hs: Assertion failed: 'a == "hello" && b > 9999999'
Assert in compile time. So code bellow should be compile error.
{-# LANGUAGE TemplateHaskell #-}
import Assertion
-- NOTE: Compile error (Good!)
main :: IO ()
main = do
-- NOTE: Compile error (Good!)
let _ = $(staticAssert (length "hello" == 99999))
putStrLn "End!"
Compile error will happen. Good!
examples/StaticAssertExample.hs:9:13: error:
• Compile time assertion failed
• In the untyped splice: $(staticAssert (length "hello" == 99999))
|
9 | let _ = $(staticAssert (length "hello" == 99999))
|