BT

The "use" Binding In F# and How It Should Be Applied To C# and VB

| by Jonathan Allen Follow 636 Followers on Aug 17, 2007. Estimated reading time: 1 minute |

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.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

First .Net language to support Generics by Eirik Mangseth

What year was F# created? Eiffel (www.eiffel.com) was one of the first languages to
be released on the .Net platform and Eiffel had support for generics long before .Net came on the market.

EM

Just Do It Please! by Kaveh Shahbazian

Just do it please! These thing are even trivial to implement.
(Personally I prefer F# to be released as a Microsoft product.)

Re: First .Net language to support Generics by Jonathan Allen

I did mis-speak. F# was the first to use ".NET Generics" as defined by the CLR.

Eiffel does have its own implementation of generics. According to the Eiffel documentation,

The CLR does not support generics at all, so that the following Eiffel for .NET classes:


This leads me to believe that Eiffel hasn't implemented .NET generics yet.

Re: Just Do It Please! by Jonathan Allen

You can post suggestions to Microsoft at connect.microsoft.com

I have taken the liberty of making a suggestion to the VB team.

connect.microsoft.com/VisualStudio/feedback/Vie...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

4 Discuss
BT