Skip to content

[FS-1047] - match! (match-bang)#4427

Merged
KevinRansom merged 26 commits into
dotnet:masterfrom
jwosty:match-bang
May 25, 2018
Merged

[FS-1047] - match! (match-bang)#4427
KevinRansom merged 26 commits into
dotnet:masterfrom
jwosty:match-bang

Conversation

@jwosty

@jwosty jwosty commented Mar 4, 2018

Copy link
Copy Markdown
Contributor

RFC FS-1047

I've implemented a version of match! that works on my fork. However, since I've been working on Mono & OSX, and I do not have access to a Windows machine for the next few days, I haven't been able to write any kind of tests for this (though I'm not sure where they would go, anyway). Any help or pointers on this matter would be much appreciated (e.g. where are the computational expression tests)

@msftclas

msftclas commented Mar 4, 2018

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@jwosty

jwosty commented Mar 4, 2018

Copy link
Copy Markdown
Contributor Author

Here's a quick little sample program using the new syntax for anyone to reference: https://gist.github.com/jwosty/bc7e1723477ea4c300e1fe6a2581d687

@cartermp

cartermp commented Mar 4, 2018

Copy link
Copy Markdown
Contributor

@jwosty Thanks so much for the contribution! I'm sure @dsyme will offer a thorough review here, and likely have additional ideas about tests.

For now, you can add some tests here: https://github.com/Microsoft/visualfsharp/tree/master/tests/fsharp/core/patterns

Comment thread tests/fsharp/core/patterns/test.fsx Outdated
Async.Sleep 1
return Hearts }

Async.RunSynchronously <| async {

@forki forki Mar 5, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please reverse it so that it does not use <|

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

Alright, trying out adding some tests.

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

cool stuff. would love to use it

/cc @tpetricek

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

Me too :) I have a lot of use cases for this in my own projects

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

same here. lots and lots of let! followed by match - but inside task { }. since you did not touch FSharp.Core (so no change to async builder) I assume it would automatically work for tasks and promise builder as well?

@realvictorprm

Copy link
Copy Markdown
Contributor

@jwosty ping me for a review, I'll also test it.

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

Yep, it works with any computation expression. It's purely a compiler trick that desugars it into a let! and a normal match

EDIT: technically, it's actually a function

@realvictorprm

Copy link
Copy Markdown
Contributor

@jwosty that's usually the trick with compilers 😄
Reminds me of the string interpolation. It's just a desugaring to string.concat with calls to "ToString" 😉

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@jwosty that's pretty nice!

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

@realvictorprm I'd love that; I don't actually have easy access to a Windows machine (so its a little trickier to pull off the testing)

@forki sweet and simple does the trick! In fact I originally suggested having it optionally emit a new computation builder method (BindPattern or something), but @dsyme wanted to go the simpler route. Makes sense in the end; doesn't require any core library changes

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

Also something to keep track of: What tooling changes should take place that would correspond to this? Which repos?

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@jwosty what kind of tooling changes to you mean?

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

@forki As in, IDE stuff. E.g. syntax highlighting doesn't recognize it (at least in the way it appears in VS for Mac). Actually, syntax highlighting might be it -- I can't think of how it would effect intellisense

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@jwosty I think VS for Mac can't show it as keyword until @nosami updates the FCS that will bring your changes. So that will take a while.

@vasily-kirichenko do you know where FCS taks keywords from?

@jwosty

jwosty commented Mar 5, 2018

Copy link
Copy Markdown
Contributor Author

@forki I see

@auduchinok

auduchinok commented Mar 5, 2018

Copy link
Copy Markdown
Member

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@nosami

nosami commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@jwosty @forki keyword intellisense and syntax highlighting doesn't come from FCS in VS for Mac. It needs to be done separately from any work that is done here. I can add it though once this PR lands.

@forki

forki commented Mar 5, 2018

Copy link
Copy Markdown
Contributor

@nosami ideally you should change it to FCS :P

@jwosty

jwosty commented Apr 2, 2018

Copy link
Copy Markdown
Contributor Author

@dotnet-bot test this please

@Thorium

Thorium commented Apr 17, 2018

Copy link
Copy Markdown
Contributor

Nice!
Just one question: Why this is match! and not with!?

@abelbraaksma

abelbraaksma commented Apr 17, 2018

Copy link
Copy Markdown
Contributor

Just one question: Why this is match! and not with!?

@Thorium, that seems just logical to me. I mean, the keyword with has multiple meanings (type extensions, copy-and-update record expressions, object expressions, match expressions), the keyword match doesn't. Using match! instead of with! keeps things simple and clear.

Might be interesting to consider function! though, as it is often used as shorthand for match. Though I'm not sure about the particulars.

But apparently I repeat myself and there's already an answer to that from @jwosty:

As for function!, I feel that would have to be another feature suggestion at this point. [...] Also, the semantics of that would warrant a discussion [...]

@jwosty

jwosty commented Apr 17, 2018

Copy link
Copy Markdown
Contributor Author

@Thorium Because match! looks similar to the other CE constructs; the "bang" always comes before the element that you're binding, never after. To me, this:

let! x = expr1
match! expr2 with
| ...

looks more consistent, and thus more visually pleasing, than this:

let! x = expr1
match expr2 with!
| ...

@abelbraaksma I agree, it would be interesting to discuss that -- you should consider starting an fslang-suggestions thread

@Thorium

Thorium commented Apr 17, 2018

Copy link
Copy Markdown
Contributor

@jwosty I was thinking of

match expr2 with!
| ...

similarity of what could be

try expr2 with!
| ...

when there is also a finally!

@forki

forki commented Apr 17, 2018 via email

Copy link
Copy Markdown
Contributor

@PatrickMcDonald

Copy link
Copy Markdown
Contributor

As Yoda might say:

do or !do, there is no try

@cartermp cartermp added this to the 15.8 milestone May 20, 2018
@dsyme

dsyme commented May 21, 2018

Copy link
Copy Markdown
Contributor

@jwosty Could you merge with master? thanks

@cartermp

Copy link
Copy Markdown
Contributor

Something is off here, there are far too many changes getting merged in

@jwosty

jwosty commented May 21, 2018

Copy link
Copy Markdown
Contributor Author

@cartermp Ah, that's because I did a squash merge. I should probably roll it back and redo it without squashing. My bad.

For cleaner history, I'm gonna force push to delete the merge commit

@jwosty

jwosty commented May 21, 2018

Copy link
Copy Markdown
Contributor Author

@dsyme Merged.

@forki

forki commented May 22, 2018 via email

Copy link
Copy Markdown
Contributor

@jwosty

jwosty commented May 22, 2018

Copy link
Copy Markdown
Contributor Author

@forki I mean, I've literally written multiple instances today that need it! I cannot wait to be spoiled by this addition.

@KevinRansom KevinRansom merged commit e33831a into dotnet:master May 25, 2018
@KevinRansom

Copy link
Copy Markdown
Contributor

Thank you for this.

@jwosty

jwosty commented May 25, 2018

Copy link
Copy Markdown
Contributor Author

@KevinRansom of course! Glad to help F# move forward.

@forki

forki commented May 25, 2018 via email

Copy link
Copy Markdown
Contributor

type CardSuit = | Hearts | Diamonds | Clubs | Spades
let fetchSuit () = async {
// do something in order to not allow optimizing things away
Async.Sleep 1

@jwosty jwosty Jul 30, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed -- it should be do! Async.Sleep 1.

On second thought I'm not sure this is needed at all -- the compiler can't doesn't really optimize let foo () = async { return 42 }, does it?

@jwosty jwosty deleted the match-bang branch May 22, 2020 23:07
T-Gro pushed a commit that referenced this pull request Jun 12, 2026
* Attempt add match! syntax to parser (as normal match) (does not work)

* Add name for MATCH_BANG token ("keyword 'match!')

* Make match! valid in (and only in) computational expressions

* match! works

* Add match! to xlf localization files

* Add two tests for match!

* Don't use left-pipe

* Give match! keyword a description

* Fix syntax error, and change match! description

* xlf updated

* Add match! keyword description to resx

* Update FSComp.fs

* Write quotation test for match!

* First crack at compile error tests

* Fix baselines

* Fix baseline one more time

* Add vs baseline

* Fix merge mistake

* Fix merge mistake (#2)

* Fix merge mistake in tests
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.