-
Notifications
You must be signed in to change notification settings - Fork 146
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
Comments
actually this could have never worked :( |
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? |
@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. |
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. |
I think the only thing missing in the original implementation is the value passed to failwithf fmt None. |
I suppose I’m not sure why you would want the format, as the failure case will always be None, won’t it? |
Or it needs to take another parameter and pass it after fmt. |
Description
I am trying to use this function
FSharpx.Extras/src/FSharpx.Extras/ComputationExpressions/Monad.fs
Lines 236 to 239 in 0c34475
but it seems to me that it does not do what I expect it to do... consider this snippet that fails to compile:
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
The text was updated successfully, but these errors were encountered: