InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

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

Posted by Jonathan Allen on Aug 17, 2007

Sections
Development,
Architecture & Design
Topics
.NET ,
Programming
Tags
F# ,
Design Patterns ,
C# ,
Visual Basic.NET

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.

First .Net language to support Generics by Eirik Mangseth Posted
Re: First .Net language to support Generics by Jonathan Allen Posted
Just Do It Please! by Kaveh Shahbazian Posted
Re: Just Do It Please! by Jonathan Allen Posted
  1. Back to top

    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

  2. Back to top

    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.)

  3. Back to top

    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.

  4. Back to top

    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...

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.