如何 return 来自具有 EXCEPTION 效果的 PureScript 函数的值?
How do I return a value from a PureScript function with an EXCEPTION effect?
我刚刚开始学习 PureScript 效果,但我一直在尝试制作具有 EXCEPTION 效果的函数。
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"
当我尝试 运行 时,出现以下错误
无法匹配类型
String
with type
Eff
( err :: EXCEPTION
| eff0
)
String
我知道 lengthGt5 在非异常情况下需要 return 一个包裹在 Eff 中的字符串,但我不确定如何围绕值 a
。我考虑的对吗?
我想通了我错过了什么。要 return 非异常情况下的值,您必须调用 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)
pure
在Applicative类型中定义class定义如下:
class (Apply f) <= Applicative f where
pure :: forall a. a -> f a
Applicative is a subclass of Apply and defines the pure function. pure
takes a value and returns a value whose type has been wrapped with the
type constructor f.
因此 pure
取值 a
,并且 return 将该值包装在类型构造函数中 - 在这种情况下,类型构造函数是 Eff e
我刚刚开始学习 PureScript 效果,但我一直在尝试制作具有 EXCEPTION 效果的函数。
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"
当我尝试 运行 时,出现以下错误
无法匹配类型
String
with type
Eff
( err :: EXCEPTION
| eff0
)
String
我知道 lengthGt5 在非异常情况下需要 return 一个包裹在 Eff 中的字符串,但我不确定如何围绕值 a
。我考虑的对吗?
我想通了我错过了什么。要 return 非异常情况下的值,您必须调用 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)
pure
在Applicative类型中定义class定义如下:
class (Apply f) <= Applicative f where
pure :: forall a. a -> f a
Applicative is a subclass of Apply and defines the pure function. pure takes a value and returns a value whose type has been wrapped with the type constructor f.
因此 pure
取值 a
,并且 return 将该值包装在类型构造函数中 - 在这种情况下,类型构造函数是 Eff e