-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for mixed async/nonasync branches in thernary if clause (corresponding to C#) #937
Comments
Basically, the current way of approaching this is to introduce nested CEs: async {
let! v1 = async { if cond1 then return! doSmthAsyncReturningInt() else return 0 }
let! v2 = async { if v1 > 77 then return! doSmthAsyncReturningInt() else return 1 }
let! v3 = async { if v2 > 88 then return! doSmthAsyncReturningInt() else return 2 }
return v3
} ... while in C#, there's no such thing as nested async states within one method. I'm not sure about the syntax that allow different types (wrapped vs. unwrapped) as proposed by @Lanayx (e.g. why only async but not other CEs? Is there a better syntactic proposal to make the typing consistent?), but at least, can we do something in the compiler so that nested CEs can be integrated with the outer one? |
Just an update - it seems we can close the issue once dotnet/fsharp#6811 is merged, since nested CE won't create an overhead anymore (for tasks). we'll just need to change async to task to get rid of the overhead. |
Just to mention this is not completely the case - nested CEs will still create a sort of overhead even for tasks (another state machine + method, though stack allocated initially). For CEs like Async where there is allocation or boxing this will minimally involve those allocations. |
Ideally you would have binds under let bindings bubble up to the nearest CE, how feasible that is I don't know. |
Title of Suggestion
I propose we add a possibility of adding synchronous branch to the if statement inside async/task CE. This is extremely helpful in caching cases when cache can be got synchronously otherwise asynchronous call is made. This can already be achieved in C#
There are two way of approaching this problem in F#:
(sharplab)
Pros and Cons
The advantages of making this adjustment to F# are - to conform convenient C# way of dealing with cache and async
The disadvantages of making this adjustment to F# are - it's unclear which syntax should be used for that, with C# it's very straitforward since await is standing near the called function, with F# it on the other side of the equal sign, so it might look like this
Extra information
Estimated cost (M):
Related suggestions: (#863 as some initial conversation happened there, #581)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: