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

若如初见. 提交于 2020-01-02 02:46:09

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/28411297/building-either-or-result-on-top-of-choice-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!