Skip to content

Conversation

@joaomatossilva
Copy link

@joaomatossilva joaomatossilva commented Oct 26, 2024

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

@Jack-Edwards
Copy link

This would be a great addition to the library.

@MPapst
Copy link

MPapst commented Nov 10, 2024

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.

@Jack-Edwards
Copy link

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

@joaomatossilva
Copy link
Author

joaomatossilva commented Nov 10, 2024

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
Copy link
Author

joaomatossilva commented Nov 10, 2024

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));

@drauch
Copy link

drauch commented Nov 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants