haskell - Haskell - 如何将 Either 返回到我的自定义类型?
问题描述
我和 GHC 还不是朋友。我希望它了解我的返回类型是我的自定义类型。
具体来说,我想确保编译器实际上返回了 OAuth2Response。有没有一种惯用的方法来实现这一目标?
例子:
data OAuth2Response = Either OAuth2Error OAuth2AccessToken
getAuthorized :: (..) -> (..) -> OAuth2Response
getAuthorized = do
a <- nonceValidation (Returns Left OAuth2Error or Right "")
b <- getAccessToken (Returns Left OAuth2Error or Right OAuth2AccessToken
b
编译器错误信息
• Couldn't match type ‘Either OAuth2Error OAuth2AccessToken’
with ‘OAuth2Response’
解决方案
data OAuth2Response = Either OAuth2Error OAuth2AccessToken
我不认为这个定义和你想的一样。特别是,Either
这里不是指Prelude 中定义的Either
类型构造函数,而是指作为此声明的一部分定义的数据构造函数。
我认为你的意思是
type OAuth2Response = Either OAuth2Error OAuth2AccessToken
即为Either
您感兴趣的特定类型定义类型同义词。
推荐阅读
- python - “'Player'对象没有属性'rect'”即使我给它属性也会发生错误
- dolphindb - 如何将int类型的时间列转换为23:53:20.081?
- reactjs - Nginx 未在 React 应用程序中显示自定义“未找到页面”页面
- python - curl 到 python 转换的问题(multipart/form-data)
- types - 为什么 Idris 中的一种特殊形式的 cong 无法进行类型检查?
- tableau-api - Tableau 中的预测季节
- python - 如何将两个 url 参数添加到 Django 模板 url 标签?
- c++ - openmp 在 c++ 中有用吗,因为我们可以使用线程池?
- android - 如何解决以下错误“任务':app:javaPreCompileDebug'的执行失败”?
- flutter - 如何在 2 个不同方向上滚动 Flutter PageView?