Classes implement an interface to meet the interface's contract. For example, a C# class may implement IDisposable
,
public class Resource : IDisposable
{
private MustBeDisposed internalResource;
public Resource()
{
internalResource = new MustBeDisposed();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing){
if (disposing){
if (resource != null) internalResource.Dispose();
}
}
}
To implement an interface in F#, use interface
in the type definition,
type Resource() =
let internalResource = new MustBeDisposed()
interface IDisposable with
member this.Dispose(): unit =
this.Dispose(true)
GC.SuppressFinalize(this)
member __.Dispose disposing =
match disposing with
| true -> if (not << isNull) internalResource then internalResource.Dispose()
| false -> ()