You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using Task<OneOf<>> frequently in our codebase and I wish there was a way to add this implicit cast from OneOf<> to Task<OneOf<>> but we C# doesn't allow adding implicit casts outside of declaring class (i.e. OneOf<>). This is because we have a use case where we use .Switch and in one branch of OneOf we run an async lambda and in the second branch of OneOf we run a sync operation just returning the result. We can use Task.FromResult in the second lambda but I just wish there was a implicit cast from OneOf of anything to Task<OneOf>
I disagree that this should be an implicit cast. In my opinion, implicit casts should be reserved for when a particular thing and a particular other thing are synonymous in some manner, and it should always be from more specific to less specific (e.g. a name object which contains first + last may be implicitly cast to string).
I would argue that OneOf<T0> and Task<OneOf<T0>> are not the same - the former is a value container and the latter is the promise of a value container. Additionally, we're moving from less specific to more specific.
If we were to add this, I think my preferred form would either be an explicit operator or, perhaps better, AsTask() or AsValueTask() extensions.
Something like this, either as a member or an extension method.
We are using
Task<OneOf<>>
frequently in our codebase and I wish there was a way to add this implicit cast fromOneOf<>
toTask<OneOf<>>
but we C# doesn't allow adding implicit casts outside of declaring class (i.e.OneOf<>
). This is because we have a use case where we use.Switch
and in one branch of OneOf we run an async lambda and in the second branch of OneOf we run a sync operation just returning the result. We can useTask.FromResult
in the second lambda but I just wish there was a implicit cast fromOneOf
of anything toTask<OneOf>
The text was updated successfully, but these errors were encountered: