question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option from C# Try method

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
3Rafalcommented, Dec 13, 2020

@gusty , I guess we can close it 😃

1reaction
3Rafalcommented, Dec 8, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found