Home
Insert
Update snippet 'Finalizable function object'
Title
Description
The snippet shows how to create an F# function value that will call a custom finalizer function when garbage collected. This is done by creating a type that inherits from FSharpFunc.
Source code
/// A type that inherits from FSharpFunc type (F# functions) /// and overrides the Finalize method with custom handler type FinalizableFunc<'T, 'R>(f, finalizer) = inherit Microsoft.FSharp.Core.FSharpFunc<'T, 'R>() override x.Invoke(a) = f a override x.Finalize() = finalizer() /// Create a function with custom finalizer let finalizable (f:'T -> 'R) handler = // F# does not allow direct cast, so we need to box let ff = FinalizableFunc<'T, 'R>(f, handler) box ff :?> ('T -> 'R) /// Create finalizable function & call it let foo () = let f = finalizable (fun n -> n + 1) (fun () -> printfn "bye!") f 1 /// The function will be collected on GC.Collect call foo () System.GC.Collect()
Tags
function
gc
finalizer
function
gc
finalizer
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update