uhlenheuerdotnet
programming, .NET, linux and others

F# - Static generic classes

by Gregor Uhlenheuer on May 6, 2012

Recently I tried to build a static generic class in F# but as it seems there is no direct way to do it. To see what I want to build, here is what could look like in C#:

public static class ProcessorUnit<T>
    where T : IProcessor
{
    public static bool Process(T element)
    {
        return element.DoWork();
    }
}

The closest I could get in F# looks like the following. Since there are no static classes in F# at all you have to use a type with a private constructor and static member definitions:

type ProcessorUnit<'T when 'T :> IProcessor> private() =

    static let proc (element : 'T) =
        element.DoWork()

    static member Process(element) =
        proc element

This post is tagged with f#, .net and generics