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

Option.getOrFailf - how to use this function? #357

Open
pmbanka opened this issue Sep 18, 2017 · 7 comments
Open

Option.getOrFailf - how to use this function? #357

pmbanka opened this issue Sep 18, 2017 · 7 comments

Comments

@pmbanka
Copy link

pmbanka commented Sep 18, 2017

Description

I am trying to use this function

let inline getOrFailF fmt =
function
| Some x -> x
| None -> failwithf fmt

but it seems to me that it does not do what I expect it to do... consider this snippet that fails to compile:

let x = Some 1

let _ = x |> Option.getOrFailF "asdf %b" true // error FS0001: Type mismatch. Expecting a     'int option -> 'a'  but given a    'bool -> 'b'

let _ = x |> (Option.getOrFailF ("asdf %b" true)) // error FS0003: This value is not a function and cannot be applied

My question is - is this an expected behaviour of this function? How should I use it correctly?

I know there is a workaroud like that:
let _ = x |> Option.getOrFail (sprintf "asdf %b" true)
but I wanted to avoid writing sprintf, hence the question.

Pinging @sideeffffect

@sideeffffect
Copy link
Contributor

sideeffffect commented Sep 18, 2017

actually this could have never worked :(
I'm removing it: #354
but thanks for trying it out!

@gdziadkiewicz
Copy link
Collaborator

I managed to draft working implementation:

> let x = Some 5
- let y : int option = None
- let inline getOrFailF fmt =
-         let f s o =
-             match o with
-             | Some x -> x 
-             | None -> failwith s
-         Printf.ksprintf f fmt  
- ;;
val x : int option = Some 5
val y : int option = None
val inline getOrFailF : fmt:Printf.StringFormat<'a,('b option -> 'b)> -> 'a

> x |> getOrFailF "asdf %b" true;;
val it : int = 5

> y |> getOrFailF "asdf %b" true;;
System.Exception: asdf true
   at FSI_0014.it@33-3.Invoke(String s, FSharpOption`1 o) in c:\Repos\FSharpx.Extras\src\FSharpx.Extras\ComputationExpressions\Option.fs:line 0
   at <StartupCode$FSI_0014>.$FSI_0014.main@()
Stopped due to error

@pmbanka Would you be still interested in having this in FSharpx.Extras?

@pmbanka
Copy link
Author

pmbanka commented May 14, 2020

@gdziadkiewicz I'm not involved in maintaining FSharpx, I was just a passerby :) So I guess this is a question for @sideeffffect.

FWIW, this looks to me like a nice useful helper that should go into the library.

@gdziadkiewicz
Copy link
Collaborator

I'm reluctant to add it in the form I proposed as it is misleading in terms of performance. It does the string printing thing in both the Some and None branches, and this is not what the user expects. I'm willing to accept an implementation that is not flawed in this way.

@panesofglass
Copy link
Contributor

panesofglass commented Jun 6, 2020

I think the only thing missing in the original implementation is the value passed to failwithf fmt None.

@panesofglass
Copy link
Contributor

I suppose I’m not sure why you would want the format, as the failure case will always be None, won’t it?

@panesofglass
Copy link
Contributor

Or it needs to take another parameter and pass it after fmt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants