ndmitchell / extra

Extra Haskell functions
Other
93 stars 37 forks source link

Add assertIO function #81

Closed Mathnerd314 closed 3 years ago

Mathnerd314 commented 3 years ago

This is a function similar to errorIO that allows using assertions as actions:

main = do
  x <- getLine -- a complex computation
  assertIO (length x > 3)

It seems to optimize away like the original assert, at least for the simple program above. I'm not sure why errorIO has a NOINLINE but I left it out here because it would remove the optimization property.

ndmitchell commented 3 years ago

Thanks! Seems a very sensible function to include. The NOINLINE on error is because otherwise the rules of IO exceptions might mean that the error moves location, and thus fires when people don't expect it. Leaving it out for assert, given the special assertion handling, makes a lot of sense.