Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access Dispose method in F# class

Tags:

f#

dispose

I have created a class in F# that implements the IDisposable interface. The class is cleaned up correctly and the use keyword is able to access the Dispose method. I have a second use case where I need to explicitly call the Dispose method and am unable to in the example below. It appears as though the Dipose method isn't available in the class.

open System

type Foo() = class
    do
        ()

    interface IDisposable with
        member x.Dispose() =
            printfn "Disposing"

end

let test() =
    // This usage is ok, and correctly runs Dispose()
    use f = new Foo()


    let f2 = new Foo()
    // The Dispose method isn't available and this code is not valid
    // "The field , constructor or member Dispose is not defined."
    f2.Dispose()

test()
like image 369
Darren Avatar asked Jan 22 '13 03:01

Darren


People also ask

What will happen if we call Dispose () method directly?

The Dispose() methodThe Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.

What happens if Dispose is not called?

Implement a finalizer to free resources when Dispose is not called. By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.

WHO calls Dispose method?

NET Garbage Collector (GC). Before the GC deallocates the memory, the framework calls the object's Finalize() method, but developers are responsible for calling the Dispose() method. The two methods are not equivalent. Even though both methods perform object clean-up, there are distinct differences between them.

Do we need to call Dispose method in C#?

Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown: using (var reader = conn.


2 Answers

Implementing an interface in an F# class is more similar to the explicit interface implementation in C#, which means that the methods of the interface do not become public classes of the method. To call them, you need to cast the class to the interface (which cannot fail).

This means that, to call Dispose you need to write:

(f2 :> IDisposable).Dispose()

In practice, this is not needed very often, because the use keyword ensures that Dispose is called automatically when the value goes out of scope, so I would write:

let test() =
  use f2 = new Foo()
  f2.DoSomething()

Here, f2 gets disposed when the test function returns.

like image 96
Tomas Petricek Avatar answered Sep 24 '22 21:09

Tomas Petricek


Tomas has it right. FYI, you can implement a Dispose() function on your type for ease of use:

member x.Dispose() = (x :> IDisposable).Dispose()

That's outside the implementation of IDisposable. Then you can just write f2.Dispose().

like image 32
Curt Nichols Avatar answered Sep 20 '22 21:09

Curt Nichols