Option from C# Try method
See original GitHub issueWhen working with C# APIs there are cases when I have an interface
public interface IValueGetter
{
bool TryGetValue(out var myValue);
}
So I map it to option like
let optionFromTryGet = function
| (true, x) -> Some x
| _ -> None
let optValue = getter.TryGetValue() |> optionFromTryGet
I wonder if it can be generalized to Option extension in F#+.
Something like Option.fromTry
implemented as above helper function.
There are similar implementations already floating around on the internet. For example this one, which is dedicated to parsing: http://www.fssnip.net/2y/title/Functional-wrappers-for-TryParse-APIs
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Try catch statements in C
The try block then uses an if/else to skip the code block to the catch block which check the local variable to see...
Read more >What is the most elegant way to write a "Try" method in C# 7?
Use an Option type, which is to say an object of a type that has two versions, typically called either "Some" (when a...
Read more >Exception-handling statements - throw and try, catch, finally
Use the try statement to catch and handle exceptions that might occur during execution of a code block. The throw statement. The throw...
Read more >try, throw, and catch Statements (C++)
First, use a try block to enclose one or more statements that might throw an exception. A throw expression signals that an exceptional ......
Read more >Try or Try[Option]
Try [Option[T]] is a good thing, it reflects three kinds of outcome - error, success, no outcome. I have a special class for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gusty , I guess we can close it 😃
Also, this pattern was described by Scott Wlaschin here: https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/completeness-seamless-dotnet-interop.html
He uses couple of different methods with out variable and binds them to
bool * 'a
tuples. But, he doesn’t show that you have to pattern match on these tuples to use them. What I would like to do is to pipe them directly to Option monad, so that the handling is easier.