在实现自己的工具模块和功能时,我想确保我的函数在特定输入上失败,例如assertFail (head [])
,但是该assertFail
函数似乎无法在Haskell中实现,有什么方法可以破解它?
If I understand what you want correctly, you can use spoon :
import Control.Spoon
assertFail x = case spoon x of
Nothing -> ()
Just _ -> error "unexpected success"
A relevant question: Is spoon unsafe in Haskell?
One way to prototype assertFail
could be assertFail = undefined
, because evaluating it would raise a runtime exception . Another would be using error
.
If you're building a test framework, a proper design would likely involve total functions (that never raise a runtime error) which return something like Maybe String
for a possible error message.
If what you need is indeed assertRaises
, take a look at Control.Exception
which likely would help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.