Consider the following example F# code:
type mytype() =
member this.v = new OtherClass()
If OtherClass implements IDisposable, does the member binding act like a let binding or a use binding? Is there any way to get it to act as a use binding? I have some code very similar to this and I want to insure that the Dispose is called when the parent object goes out of scope.
A quick scan through Expert F# failed to turn up anything definite but perhaps I am looking for the wrong terms in the book.
In your snippet, the body of the member
vwill be evaluated each time the member is called (meaning that it will create a new instance ofOtherClasseach time someone uses it). The member simply returns the newly created object and you could use it like this:I’m not sure if this is what you were asking though. If you want to create only a single instance, you’ll need to write something like this:
This behaves as usual
letbinding and thevvalue won’t be disposed automatically when the current instance ofMyTypeis disposed. To havevautomatically disposed, you’ll need to implementIDisposableinMyTypeexplicitly:Unfortunately, there is no syntactic sugar that would make it nicer (I once suggested allowing
useand implicitly implementingIDisposablefor type members to the F# team, but they didn’t implement it(yet:-))).