1

Right now, when I have a Result type, it seems like I need to do a match to find if it is ok, or not.

So, I am trying to simplify this to be able to use if, like that:

let a : Result<string, string> = ...
if a.IsOk then ....

Here is my attempt

[<AutoOpen>]
module ResultHelper =
    type Result with
        member this.IsOk =
            match this with
            | Ok _    -> true
            | Error _ -> false

but this will not compile.

Using:

    type Result<_, _> with

doesn't help either

How can I achieve this?

Thomas
  • 10,933
  • 14
  • 65
  • 136
  • Note that `Result.map` does more or less exactly what you are trying to do here. – Guran Nov 24 '20 at 14:45
  • @Guran I had no idea result.map was a thing, but it looks like it's exactly what I need since I need to chain operations. thanks! – Thomas Nov 24 '20 at 19:28
  • you’re welcome. Map and Bind are really nice constructs. Just beware the M-word... – Guran Nov 24 '20 at 19:33

1 Answers1

3

The correct syntax is to give the type parameters names. As a bonus, this will allow you to refer to them in the code if you need to.

type Result<'a, 'b> with
    member this.IsOk =
        match this with
        | Ok _    -> true
        | Error _ -> false

P.S. For the future, please note that "will not compile" is not a good description of a problem. Please make sure to always include the full error message.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172