-
Notifications
You must be signed in to change notification settings - Fork 21
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 match! (match-bang) syntactic sugar to computation expressions [ RFC FS-1047 ] #572
Comments
match!
syntactic sugar to computation expressions
Looks like a duplicate of this #316 |
@dsyme looks like you're right, but since Joinads was rejected, could we reconsider? (e.g. reopen the older proposal or keep this one) |
@cartermp I could see that being useful, but I'd much prefer having |
In the resolution of https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663965-integrate-joinads-extension it was mentioned that that proposal was being declined "for now". Does that mean that it might be resurrected at some point in the future? If so, it might be wise to hold the However, if the decision was that joinads would never be integrated, then there's no issue with re-evaluating #316 here. |
@cartermp While one could write a builder for each monad stack one needs, I find myself repeating that I often pattern match on domain models, not just on built-in types like type Score =
| Points of PointsData
| Forty of FortyData
| Deuce
| Advantage of Player
| Game of Player As a 'naked' discriminated union, it's not monadic (it's not even a functor), so I'm not sure it's possible to write a computation expression builder for such a type. You might still be able to write one for a combination that involves a proper monad like It's possible that I could write a computation expression builder for e.g. |
@rmunn ah, didn't catch that joinads was rejected "for now." Therefore the disadvantage to implementing this suggestion is that it's pretty incompatible, thus if it were implemented, it would prevent Joinads from becoming a thing ever. I wonder if that's possible to avoid somehow (if Joinads is a possibility eventually). |
Is there anything preventing this from being marked as approved-in-principle? |
I'm interested in a statement too. What are the real cons of this? For me it looks like a nice-to-have addition :thinking_face: |
Marking as approved-in-principle per discussion in language-design office hours
|
From language design office hours
|
Oooh man, does this mean we're going to get idris-style inline bind? That would be so rad. |
@Rickasaurus I'm not familiar with Idris; what's inline bind? Edit: Ah, I see what you're referencing. There's no suggestion for that feature yet -- nobody is stopping anyone from writing one! ;) |
@Rickasaurus That's a cool language feature! |
@jwosty I know right? Idris got it like five years ago and I've been drooling over it ever since. |
@Rickasaurus I'm considering opening a feature request for this later. Syntactic sugar FTW! |
Implementation has been submitted |
Just found that
results in:
and the simplest valid code seems to be:
which is suboptimal. Would it be possible to improve this? |
I certainly have no objections to allowing assignment of the result. @jwosty, would you have any concerns? |
This is by-design, and nothing to do with For example
does not compile, you must wrap the r.h.s. of the |
I think the unexpected bit is that because it's syntax sugar for this: let asyncX = async { return Some 12 }
async {
let! x = asyncX
let res =
match x with
| Some v -> v
| None -> 12
return res
} folks like @et1975 would expect to assign the result. |
Right, but the right-hand side of Specifically, in this context: |
@et1975 Yes, it's somewhat large transition. So
becomes
and
becomes
I suppose we could in theory allow |
Can we also apply that to the function syntax as well? Example:
|
@bizmonger, i believe that was considered, but eventually not included, but I don't remember the reasoning. You should probably open a new language suggestion, a closed one is not likely to be picked up. |
Add a
match!
syntactic sugar to computation expressionsWhen writing computation expressions, especially asyncs that call my own functions returning
Option
s, I find myself quite often binding it (vialet!
), then proceeding to use that variable only one time in a pattern match immediately following. It'd be really nice to eliminate the unnecessarylet!
and have a sort ofmatch!
that means the same thing as alet!
followed by a vanillamatch
.For example, this:
could instead be written as:
To implement this, the compiler could by default treat it the same as
let!
followed bymatch
,or spit out a call to a new builder method for this construct if the builder defines it, perhaps calledBindPattern
. It would be identical in signature tobuilder.Bind
.E.g. compiler could emit
or
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: