Help: returning a different OneOf from within a Match
See original GitHub issueHi Harry
I’m new to OneOf and am having a play with it at work, mainly using it as an Option type via OneOf<T, None>
.
I have a couple of questions - 1. how do we avoid massive nesting within Matches when you need to do something and then something else and then something else and then something else?
- How do I return a different OneOf<> from within a Match? My use case is this:
private async Task<OneOf<string, None>> GetContent(string requestUri, CorrelationId correlationId)
{
var response = await client.GetAsync(requestUri); // client is an HttpClient
var content = response.Content == null ? string.Empty : await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return content;
}
return new None();
}
public async Task<OneOf<Address, None>> AddressSearch(string term, CorrelationId correlationId)
{
var requestUri = AddressSearchUri
.AddQueryParam("client_id", clientId)
.AddQueryParam("term", term);
var content = await GetContent(requestUri, correlationId);
return content.Match(
str =>
{
var addressSearchResponse = str.FromJson<AddressSearchResponse>();
if (addressSearchResponse?.RetrieveLocationResponse?.BusinessResult?.Success != "Y")
{
return new None();
}
if (addressSearchResponse?.RetrieveLocationResponse?.TypeAheadSearch == null)
{
return new None();
}
return new Address( // this line fails to compile - "Cannot implicitly convert type Address to OneOf.Types.None"
addressSearchResponse.RetrieveLocationResponse.TypeAheadSearch[0].Elid,
addressSearchResponse.RetrieveLocationResponse.TypeAheadSearch[0].Label);
},
none => new None()
);
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
How can I return true only if one of a set of strings matches?
I'd probably go with String#scan and a simple regex so that you can understand what you've done later:
Read more >Using the return operator in the match expression. Early return ...
Hello everyone. I can't understand why the early return of an error in the expression match is needed. How does it differ from...
Read more >rust - Can an object be moved through a match expression?
However, I'd like to NOT destructure it, but only match against it and dispatch the complete object (using move semantics) to one of...
Read more >MATCH function
How to use the MATCH function in Excel to search for a specified item in a range of cells, returning the relative position...
Read more >How to proceed when Match activity fails to return any result
Hi there, I'm currently working on transposing data from multiple PDF invoices to an Excel table. “Match” activity with Regex expressions ...
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 Free
Top 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
@mattfrear didn’t see your message before I replied, you’re welcome!
I would say early return would not be particularly idiomatic in a functional language if we want to compare this to F#. Even though there are some parts of the language where multiple return statements are used. https://fsharpforfunandprofit.com/series/computationexpressions.html
If you try to use the the IsTX methods you loose most of the type safety benefits of this library. They shouldn’t really be used for purposes other than extensions such as Serialisation. They should be hidden somehow in my option as them being there encourages people to miss-use this library.
My guidelines would be to be when you are creating a OneOf type use multiple return statements, but when you are consuming them you should use only one return statements. This forces the consumer to handle all the cases inside the OneOf! Avoiding the pitfalls of the is/as or multiple if statement approach!
@mattfrear last day at RP today actually! Glad to hear you hit the ground running.
I reckon the
.TryPickTX
methods are what you are after. This is from the readme:This lets you remove a single case at a time, and process the OneOf without deep nesting