Skip to content

Conversation

@ncave
Copy link
Contributor

@ncave ncave commented Aug 17, 2017

added some missing IDisposable interfaces

@forki
Copy link
Contributor

forki commented Aug 17, 2017

Cool

@KevinRansom
Copy link
Contributor

@ncave

How would this change actually be consumed? What code today needs this change, or what will this change enable?

Thanks

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@KevinRansom Any code that obtains sequence enumerator and then tries to dispose it (e.g. with a use statement). This just fixes inconsistensies, most of the sequence enumerators already had the IDisposable interface, there were only a few that did not.

@KevinRansom
Copy link
Contributor

with this change:

The generated enumerator doesn't really need collection. It is implemented as:

member __.Dispose () = () 

Dispose may be necessary if one were to generate a derived class with an enumeration over a COM collection. But since that would be special code it should add IDisposable to the derived class.

let main argv = 
    use x = [ for x in 0 .. 10 -> x * x]

    printf "%A" x

    0 // return an integer exit code


d:\ncave\visualfsharp\release\net40\bin\temp>..\fsc Program.fs
Microsoft (R) F# Compiler version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

Program.fs(6,9): error FS0193: Type constraint mismatch. The type
    'int list'
is not compatible with type
    'System.IDisposable'

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@KevinRansom use on sequence enumerators, not lists. Most of the sequence enumerators already have the IDisposable interface (here, here, here, etc.), this just fixes the ones where it was missed.

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

@ncave Generated classes like this do all have the IDisposable interface, it's just not listed explicitly. It's implied by inheritance via the IEnumerator<T> interface. F# lets you implement inherited interfaces in the listing of methods for a sub-interface.

However it may be that Fable translation wants to see these as explicit interface implementations? I don't mind making this explicit.

The one change we can't take is that change to the public API surface of the DIspose helper function.

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

@KevinRansom As long as this change is undone then AFAICS this is simply code cleanup, making interfaces that are already implemented implicitly more explicit.

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@dsyme I'll take it out, but do you mind elaborating a bit, as this Dispose is only supposed to be called by compiled code? The change I made preserves existing functionality and fixes some use cases when code is generated to dispose things that don't implement IDisposable.

@KevinRansom
Copy link
Contributor

Oh wow ... I didn't know that about IEnumerator and I think it's kind of silly, however, are we going to teach the compiler to work for:

let main argv = 
    use x = [ for x in 0 .. 10 -> x * x]

    printf "%A" x

    0 // return an integer exit code


d:\ncave\visualfsharp\release\net40\bin\temp>..\fsc Program.fs
Microsoft (R) F# Compiler version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

Program.fs(6,9): error FS0193: Type constraint mismatch. The type
    'int list'
is not compatible with type
    'System.IDisposable'

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@KevinRansom If we add IDisposable.Dispose somewhere here, then yes, why not.

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

@dsyme I'll take it out, but do you mind elaborating a bit, as this Dispose is only supposed to be called by compiled code? The change I made preserves existing functionality and fixes some use cases when code is generated to dispose things that don't implement IDisposable.

Strictly speaking removing a generic constraint isn't a breaking change to the API of a component (other changes to method signatures would be). However this is a little subtle and I'd just rather avoid that change in what is otherwise a cleanup PR

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

however, are we going to teach the compiler to work for..

To be clear, this PR doesn't do that. IDisposable is only implied by IEnumerator<T>

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@dsyme @KevinRansom Ok, Dispose change reverted. It really does help alleviate some code generation issues on lesser platforms, so it's a nice to have, but yeah, I'll leave it at that. Thanks for reviewing!

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

@dsyme @KevinRansom Ok, Dispose change reverted. It really does help alleviate some code generation issues on lesser platforms, so it's a nice to have, but yeah, I'll leave it at that. Thanks for reviewing!

@ncave does this change help Fable in some way, e.g. by making the IDisposable implementation explicit in the generated classes?

@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@dsyme I'm sure it will, in a way, in the sense that it makes all the sequence enumerators consistent (most of them already have the explicit IDispose). It's just a code cleanup, as you said, but it also helps platforms that can't handle non-explicit interfaces.

@dsyme
Copy link
Contributor

dsyme commented Aug 17, 2017

... helps platforms that can't handle non-explicit interfaces.

So Fable can't handle non-explicit interfaces? We should add an implement-all-interfaces-explicitly option, is that already in Fable compiler?

@dsyme dsyme changed the title added missing IDisposable interfaces implement IDisposable interfaces explicitly Aug 17, 2017
@ncave
Copy link
Contributor Author

ncave commented Aug 17, 2017

@dsyme Sorry, perhaps I was unclear, wasn't talking about Fable but other projects like this.

@KevinRansom KevinRansom merged commit 124e934 into dotnet:master Aug 17, 2017
KevinRansom pushed a commit that referenced this pull request Aug 17, 2017
* Document project options and mark lots of things as deprecated (#3449)

* Document project options and mark lots of things as deprecated

* delete some code that is no longer used

* fix packaging of FSharp.Core. (#3452)

* implement IDisposable interfaces explicitly  (#3447)

* added IDisposable

* reverted Dispose change

* ngen open source install (#3456)

* handle "Blue (high contrast)" theme (#3443)
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Jan 26, 2022
* Document project options and mark lots of things as deprecated (dotnet#3449)

* Document project options and mark lots of things as deprecated

* delete some code that is no longer used

* fix packaging of FSharp.Core. (dotnet#3452)

* implement IDisposable interfaces explicitly  (dotnet#3447)

* added IDisposable

* reverted Dispose change

* ngen open source install (dotnet#3456)

* handle "Blue (high contrast)" theme (dotnet#3443)
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.

5 participants