Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option.ofPair #207

Closed
AlbertoDePena opened this issue Jan 20, 2023 · 5 comments · Fixed by #208
Closed

Add Option.ofPair #207

AlbertoDePena opened this issue Jan 20, 2023 · 5 comments · Fixed by #208
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@AlbertoDePena
Copy link
Contributor

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?

@TheAngryByrd
Copy link
Collaborator

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 TheAngryByrd added enhancement New feature or request good first issue Good for newcomers labels Jan 20, 2023
@AlbertoDePena
Copy link
Contributor Author

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

@TheAngryByrd
Copy link
Collaborator

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
Copy link
Contributor

njlr commented Jan 20, 2023

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

@AlbertoDePena
Copy link
Contributor Author

@njlr

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants