InfoQ

News

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

Posted by Jonathan Allen on Aug 17, 2007 12:27 PM

Community
.NET
Topics
Programming
Tags
Visual Basic.NET,
F#,
C#,
Design Patterns

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.

4 comments

Reply

First .Net language to support Generics by Eirik Mangseth Posted Aug 17, 2007 3:39 PM
Re: First .Net language to support Generics by Jonathan Allen Posted Aug 19, 2007 10:36 PM
Just Do It Please! by Kaveh Shahbazian Posted Aug 18, 2007 1:03 AM
Re: Just Do It Please! by Jonathan Allen Posted Aug 20, 2007 12:20 PM
  1. Back to top

    First .Net language to support Generics

    Aug 17, 2007 3:39 PM 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

  2. Back to top

    Just Do It Please!

    Aug 18, 2007 1:03 AM 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.)

  3. Back to top

    Re: First .Net language to support Generics

    Aug 19, 2007 10:36 PM 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.

  4. Back to top

    Re: Just Do It Please!

    Aug 20, 2007 12:20 PM by Jonathan Allen

    You can post suggestions to Microsoft at http://connect.microsoft.com I have taken the liberty of making a suggestion to the VB team. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=294208

Exclusive Content

Rationalizing the Presentation Tier

Thin client paradigm characterized by web applications is a kludge that needs to be repudiated. Old compromises are no longer needed and it's time to move the presentation tier to where it belongs.

Agile Project Management: Lessons Learned at Google

In this presentation filmed during QCon 2007, Jeff Sutherland, the creator of Scrum, talks about his visit at Google to do an analysis of Google's first implementation of Scrum.

AtomServer – The Power of Publishing for Data Distribution

In this article, Bryon Jacob and Chris Berry introduce AtomServer, their implementation of a full-fledged Atom Store based on Apache Abdera, which is now available as open source.

An Introduction to Virtualization

It is easy to think that virtualization applies only to servers. In reality the recent resurgence of the concept is also being applied to networking, storage, and application infrastructure.

REST Anti-Patterns

In this article, Stefan Tilkov explains some of the most common anti-patterns found in applications that claim to follow a "RESTful" design and suggests ways to avoid them.

Choosing between Routing and Orchestration in an ESB

In this article, Adrien Louis and Marc Dutoo discuss the differences and relative merits of using orchestration vs. routing in a typical ESB setup, and discuss various implementation options.

Enterprise Batch Processing with Spring

Wayne Lund discusses batch processing, Spring Batch objectives and features, scenarios for usage, Spring Batch architecture, scaling, example code, failures and retrying, and the future roadmap.

User Story Estimation Techniques

Developer Jay Fields draws on his experiences as a ThoughtWorks consultant to describe effective user story estimation techniques.