OneOf icon indicating copy to clipboard operation
OneOf copied to clipboard

Add Support for Async

Open joaomatossilva opened this issue 1 year ago • 6 comments

Created versions of Map, Switch and Match that are awaitable, for references on netstandard 2.0 or greater than .Net 4.0

joaomatossilva avatar Oct 26 '24 12:10 joaomatossilva

This would be a great addition to the library.

Jack-Edwards avatar Nov 10 '24 16:11 Jack-Edwards

You guys know that this is already possible?

OneOf.OneOf<string, uint> test = 15;

var result = await test.Match(
    async str => await Task.FromResult(str),
    async i => await Task.FromResult(i.ToString()));

Instead of switch you can also use a Match, that just returns a Task.

MPapst avatar Nov 10 '24 18:11 MPapst

await test.MapT#(async x => await ...) is what I was really hoping for. There doesn't appear to be support for it.

Jack-Edwards avatar Nov 10 '24 18:11 Jack-Edwards

You guys know that this is already possible?

OneOf.OneOf<string, uint> test = 15;

var result = await test.Match(
    async str => await Task.FromResult(str),
    async i => await Task.FromResult(i.ToString()));

Instead of switch you can also use a Match, that just returns a Task.

Yes. This should be possible with this PR.

Just a note as await Task.FromResult(str) would maybe result in a Task<Task>.

I guess what you meant was

var result = awai test.Match(
  async str => await awaitableFunction(str), // with await
  i => Task.FromResult(i.ToString())); // no await returning task

joaomatossilva avatar Nov 10 '24 21:11 joaomatossilva

await test.MapT#(async x => await ...) is what I was really hoping for. There doesn't appear to be support for it.

Yes. I think this test is exactly that. https://github.com/mcintyre321/OneOf/pull/183/files#diff-bdd8be91f178aa5135c2b1ef3e22a9f1011430668103808b41d115d8a2e57658R42

var map1 = await input.MapT0Async(d => Task.FromResult(d.ToString(CultureInfo.InvariantCulture)));

In this case is not awaiting inside the lambda, but could be written as

var map1 = await input.MapT0Async(async d => await awaitableFunc(d));

joaomatossilva avatar Nov 10 '24 21:11 joaomatossilva

Can you publish the version containing this feature to NuGet please? :-)

drauch avatar Nov 28 '24 13:11 drauch