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
You want
let Error = Choice2Of2<string>
. Note the uppercaseO
.You need also an active pattern:
Then for
Either
:That's the way I did it here.