building Either (or Result) on top of Choice in F#

2019-04-29 10:14发布

I built a monad for success/failure based on information in Scott Wlaschin's blog with extra help from this stack overflow posting. I ended up with a type

type Result<'a> = 
| Success of 'a
| Error of string

I now realize that this is equivalent of Choice in F#, and Either in haskell. I'd like to reuse code instead of keeping my own, but would like to just change the implementation and not have to change my existing code. I'd like to use my existing names along with the implementation of Choice. (Or maybe the expanded Choice in fsharpx.)

I have tried

type Result<'a> = Choice<'a, string>
let Success = Choice1Of2
let Error = Choice2Of2

This almost works, but when I use Error in a match, I get an error "The pattern discriminator 'Error' is not defined.

    match getMetaPropertyValue doc name with
    | Error msg -> ()
    | Success value -> value

标签: f# monads
2条回答
冷血范
2楼-- · 2019-04-29 10:56

You want let Error = Choice2Of2<string>. Note the uppercase O.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-29 11:04

You need also an active pattern:

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

Then for Either:

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

That's the way I did it here.

查看更多
登录 后发表回答