demystifyfp/FsToolkit.ErrorHandling

Add Option.ofPair

AlbertoDePena opened this issue · 5 comments

Is your feature request related to a problem? Please describe.

Not a problem, just a feature request

Describe the solution you'd like

Utility method to convert

static member TryParse:
   s     : string *
   result: byref<int>
        -> bool

to Option

[<RequireQualifiedAccess>]
module Option =

    let ofPair (x: bool * 'a) =
        match x with
        | true, y -> Some y
        | false, _ -> None

let toInt32 (value: string) = 
    Int32.TryParse value 
    |> Option.ofPair

"HI" |> toInt32 // None
"10" |> toInt32 // Some 10

Describe alternatives you've considered

let tryParseInt (value: string) =
    let (parsed, parsedValue) = Int32.TryParse value
    if parsed then
        Some parsedValue
    else
        None

Additional context

Please ignore if this already exists. If so, where can I find such utility method?

There isn't an ofPair but there is a tryParse with examples. That being said, this only accounts for that exact TryParse signature and doesn't work for Fable.

Definitely willing to take a PR for this. Also would be good addition to ValueOption set of functions as well.

@TheAngryByrd you mean the SRTP is not Fable compliant correct? I wouldn't mind taking a stab at the PR

Yeah I remember it having issues with fable. I haven’t tried with the 4.0 versions though.

Great. Please ask questions if you have them!

njlr commented

I was curious if this works now; it compiles but the behaviour is incorrect: fable-compiler/Fable#3332

@njlr

I think adding Option.ofPair (the name does not matter) is nice since not everyone (including me) is super comfortable with SRTP =)