jsparkes
jsparkes

Reputation: 321

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

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

Upvotes: 8

Views: 2597

Answers (2)

Gus
Gus

Reputation: 26184

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.

Upvotes: 10

user1804599
user1804599

Reputation:

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

Upvotes: 0

Related Questions