尝试扩展结果类型..在F#中失败 [英] trying to extend the result type.. unsuccesfully, in F#
本文介绍了尝试扩展结果类型..在F#中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在,当我有一个Result类型时,似乎需要进行匹配以查找是否可以.
Right now, when I have a Result type, it seems like I need to do a match to find if it is ok, or not.
因此,我正在尝试简化此操作以使其能够使用,例如:
So, I am trying to simplify this to be able to use if, like that:
let a : Result<string, string> = ...
if a.IsOk then ....
这是我的尝试
[<AutoOpen>]
module ResultHelper =
type Result with
member this.IsOk =
match this with
| Ok _ -> true
| Error _ -> false
但这不会编译.
使用:
type Result<_, _> with
也没有帮助
我该如何实现?
推荐答案
正确的语法是为类型参数命名.另外,如果需要,您可以在代码中引用它们.
The correct syntax is to give the type parameters names. As a bonus, this will allow you to refer to them in the code if you need to.
type Result<'a, 'b> with
member this.IsOk =
match this with
| Ok _ -> true
| Error _ -> false
P.S.为了将来,请注意,将不会编译"对问题的描述不是很好.请确保始终包含完整的错误消息.
P.S. For the future, please note that "will not compile" is not a good description of a problem. Please make sure to always include the full error message.
这篇关于尝试扩展结果类型..在F#中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文