Skip to content
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

Allow _ in use bindings #11630

Merged
merged 2 commits into from Jun 9, 2021
Merged

Allow _ in use bindings #11630

merged 2 commits into from Jun 9, 2021

Conversation

lostmsu
Copy link
Contributor

@lostmsu lostmsu commented Jun 4, 2021

Adds special case check for TPat_wild in TcLetBinding

Implements fsharp/fslang-suggestions#881 and https://github.com/fsharp/fslang-design/blob/6c1ba3425c6129f1b6cf22ac8ddec30761136a12/RFCs/FS-1102-discards-on-use-bindings.md

Has basic test UseBindingDiscard01, that ensures, that use _ = new Disposable() type checks

@lostmsu
Copy link
Contributor Author

lostmsu commented Jun 4, 2021

I also checked the runtime behavior in F# interactive with this:

type Disposable() =
  do printfn "constructed"
  interface System.IDisposable with
   member _.Dispose() = printfn "disposed!"

let answer =
  use _ = new Disposable()
  42

Output:

constructed
disposed!

This ensures that right the hand side of the binding is only evaluated once, and that Dispose is actually being called.

Not sure if there's a good way to make it (e.g. runtime behavior) into a feature test.

@Happypig375
Copy link
Member

Here is an example test with runtime testing. https://github.com/dotnet/fsharp/blob/a96d7ceb2a5b95cccee8d7febe4188369a8e14be/tests/FSharp.Compiler.ComponentTests/Language/CodeQuotationTests.fs

@lostmsu
Copy link
Contributor Author

lostmsu commented Jun 7, 2021

Added runtime test too.

Copy link
Contributor

@cartermp cartermp left a comment

Choose a reason for hiding this comment

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

This will require a langversion check as well.

See here for an example of how to guard for the language feature: https://github.com/dotnet/fsharp/pull/11603/files

Otherwise, this looks great, thanks!

let ``Dispose called for discarded value of use binding`` () =
Fsx """
type private Disposable() =
[<DefaultValue>] static val mutable private disposed: bool
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit weird that you check that the constructor is called exactly once but the Dispose method being called more than once still succeeds :)

@lostmsu
Copy link
Contributor Author

lostmsu commented Jun 7, 2021

@cartermp got us a langver check.

Copy link
Contributor

@cartermp cartermp left a comment

Choose a reason for hiding this comment

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

Thanks! This looks great.

@vzarytovskii vzarytovskii merged commit 412f147 into dotnet:main Jun 9, 2021
@lostmsu lostmsu deleted the UseWild branch June 10, 2022 00:30
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.

None yet

4 participants