F# 在F中的选择之上构建(或结果)#
我根据Scott Wlaschin的信息构建了一个成功/失败的monad,并从中获得了额外的帮助。我最终得到了一个类型F# 在F中的选择之上构建(或结果)#,f#,monads,F#,Monads,我根据Scott Wlaschin的信息构建了一个成功/失败的monad,并从中获得了额外的帮助。我最终得到了一个类型 type Result<'a> = | Success of 'a | Error of string 您需要让Error=Choice2Of2。注意大写的O您还需要一个活动模式: type Result<'a> = Choice<'a,string> let Success x :Result<'a> = Choice1O
type Result<'a> =
| Success of 'a
| Error of string
您需要
让Error=Choice2Of2
。注意大写的O
您还需要一个活动模式:
type Result<'a> = Choice<'a,string>
let Success x :Result<'a> = Choice1Of2 x
let Error x :Result<'a> = Choice2Of2 x
let (|Success|Error|) = function Choice1Of2 x -> Success x | Choice2Of2 x -> Error x
我就是这样做的。谢谢。我真是太蠢了。关于使用错误作为模式鉴别器,我仍然会遇到一个错误,所以我会修正这个问题。谢谢。那是丢失的一块。
match getMetaPropertyValue doc name with
| Error msg -> ()
| Success value -> value
type Result<'a> = Choice<'a,string>
let Success x :Result<'a> = Choice1Of2 x
let Error x :Result<'a> = Choice2Of2 x
let (|Success|Error|) = function Choice1Of2 x -> Success x | Choice2Of2 x -> Error x
type Either<'a,'b> = Choice<'b,'a>
let Right x :Either<'a,'b> = Choice1Of2 x
let Left x :Either<'a,'b> = Choice2Of2 x
let (|Right|Left|) = function Choice1Of2 x -> Right x | Choice2Of2 x -> Left x