Exception handling 如何从具有异常效果的PureScript函数返回值?
我刚刚开始学习PureScript效果,我一直在尝试制作一个具有异常效果的函数Exception handling 如何从具有异常效果的PureScript函数返回值?,exception-handling,effects,purescript,Exception Handling,Effects,Purescript,我刚刚开始学习PureScript效果,我一直在尝试制作一个具有异常效果的函数 lengthGt5 :: forall eff. String -> Eff (err :: EXCEPTION | eff) String lengthGt5 a = if (length a <= 5) then throwException $ error "Word is not the right length!" else a main
lengthGt5 :: forall eff. String -> Eff (err :: EXCEPTION | eff) String
lengthGt5 a = if (length a <= 5)
then throwException $ error "Word is not the right length!"
else a
main = do
word <- catchException handleShortWord (lengthGt5 "test")
log word
where
handleShortWord err = do
log (message err)
return "Defaut::casserole"
我知道Length5在非异常情况下需要返回一个封装在Eff中的字符串,但我不确定如何在值
a
周围创建一个“空效果包装器”。我想的对吗?我发现我错过了什么。要在非异常情况下返回值,必须调用pure a
lengthGt5 :: forall eff. String -> Eff (err :: EXCEPTION | eff) String
lengthGt5 a = if (length a <= 5)
then throwException $ error "Word is not the right length!"
else (pure a)
Applicative是Apply的一个子类,定义了pure函数。纯净的
获取一个值并返回一个值,该值的类型已用
类型构造函数f
因此,
pure
获取值a
,并返回封装在类型构造函数中的值-在这种情况下,类型构造函数是Eff e
注意,pure
现在被用来代替return
lengthGt5 :: forall eff. String -> Eff (err :: EXCEPTION | eff) String
lengthGt5 a = if (length a <= 5)
then throwException $ error "Word is not the right length!"
else (pure a)
class (Apply f) <= Applicative f where
pure :: forall a. a -> f a