Skip to content

Instantly share code, notes, and snippets.

@mrange
Last active October 3, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrange/82fafb1fdc8a69c1ec544858b54f40f8 to your computer and use it in GitHub Desktop.
Save mrange/82fafb1fdc8a69c1ec544858b54f40f8 to your computer and use it in GitHub Desktop.
F# Types

F# uses the type keyword to create different kind of types.

  1. Type aliases
  2. Discriminated union types
  3. Record types
  4. Interface types
  5. Class types
  6. Struct types

Examples with equivalent C# code where possible:

    // Equivalent C#:
    //  using IntAliasType = System.Int32;
    type IntAliasType = int // As in C# this doesn't create a new type, merely an alias

    type DiscriminatedUnionType =
      | FirstCase
      | SecondCase  of int*string

      member x.SomeProperty = // We can add members to DU:s
        match x with
        | FirstCase         -> 0
        | SecondCase (i, _) -> i

    type RecordType =
      {
        Id    : int
        Name  : string
      }
      static member New id name : RecordType = // We can add members to records
        { Id = id; Name = name } // { ... } syntax used to create records

    // Equivalent C#:
    //  interface InterfaceType
    //  {
    //    int     Id    { get; }
    //    string  Name  { get; }
    //    int Increment (int i);
    //  }  
    type InterfaceType =
      interface // In order to create an interface type, can also use [<Interface>] attribute
        abstract member Id        : int
        abstract member Name      : string
        abstract member Increment : int -> int
      end

    // Equivalent C#:
    //  class ClassType : InterfaceType
    //  {
    //    static int increment (int i)
    //    {
    //      return i + 1;
    //    }
    //  
    //    public ClassType (int id, string name)
    //    {
    //      Id    = id    ;
    //      Name  = name  ;
    //    }
    //  
    //    public int    Id    { get; private set; }
    //    public string Name  { get; private set; }
    //    public int   Increment (int i)
    //    {
    //      return increment (i);
    //    }
    //  }
    type ClassType (id : int, name : string) = // a class type requires a primary constructor
      let increment i = i + 1 // Private helper functions

      interface InterfaceType with // Implements InterfaceType
        member x.Id           = id
        member x.Name         = name
        member x.Increment i  = increment i


    // Equivalent C#:
    //  class SubClassType : ClassType
    //  {
    //    public SubClassType (int id, string name) : base(id, name)
    //    {
    //    }
    //  }
    type SubClassType (id : int, name : string) =
      inherit ClassType (id, name) // Inherits ClassType

    // Equivalent C#:
    //  struct StructType
    //  {
    //    public StructType (int id)
    //    {
    //      Id = id;
    //    }
    //  
    //    public int Id { get; private set; }
    //  }
    type StructType (id : int) =
      struct  // In order create a struct type, can also use [<Struct>] attribute
        member x.Id = id
      end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment