F# is a functional language that runs on the CLR. Despite being a research language, it has several claims to fame including being the first .NET language to support generics.
In a recent post about F# 1.9.2, Don Syme talked about the "use" binding. Essentially it isn't that interesting, it just provide support for the Using construct so familiar to C# and VB developers. In fact, "use" itself is not much more powerful than the "using" function that can be so easily written in F#.
As a possible future enhancement, Don Syme mentioned that the "use" binding could be applied at the class level. If it were done that way, then the IDisposable pattern could be automatically implemented by the compiler.
Before we move on, lets take a moment to look at the IDisposable pattern. Below is the code snippet that ships with Visual Basic.
Class ResourceClass Implements IDisposable Private disposed As Boolean = False ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me.disposed Then If disposing Then ' Free other state (managed objects). End If ' Free your own state (unmanaged objects). ' Set large fields to null. End If Me.disposed = True End Sub #Region " IDisposable Support " ' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub Protected Overrides Sub Finalize() ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(False) MyBase.Finalize() End Sub #End Region End Class
As you can see, there is quite a bit of overhead. Even if the class doesn't have any unmanaged resources of its own, allowing the removal of the Finalize method, there is still a lot going on. And ultimately it is still flawed because there is no error handling, which can become a major problem down the road.
This is where the impact of research languages can really be felt. By taking this idea from F# and applying it to VB and C#, we would reduce all of this boilerplate code to a single partial method that handles the unmanaged objects.