Export (0) Print
Expand All

Object Expressions (F#)

An object expression is an expression that creates a new instance of a dynamically created, anonymous object type that is based on an existing base type, interface, or set of interfaces.

// When typename is a class:
{ new typename [type-params] arguments with
   member-definitions
  [ additional-interface-definitions ]
}
// When typename is not a class:
{ new typename [generic-type-args] with
   member-definitions
  [ additional-interface-definitions ]
}

In the previous syntax, the typename represents an existing class type or interface type. type-params describes the optional generic type parameters. The arguments are used only for class types, which require constructor parameters. The member-definitions are overrides of base class methods, or implementations of abstract methods from either a base class or an interface.

The following example illustrates several different types of object expressions.


// This object expression specifies a System.Object but overrides the
// ToString method.
let obj1 = { new System.Object() with member x.ToString() = "F#" }
printfn "%A" obj1 

// This object expression implements the IFormattable interface.
let Delimiter(delim1 : string, delim2 : string ) = { new System.IFormattable with
                member x.ToString(format : string, provider : System.IFormatProvider) =
                  if format = "D" then delim1 + x.ToString() + delim2
                  else x.ToString()
           }

let obj2 = Delimiter("{","}");

printfn "%A" (System.String.Format("{0:D}", obj2))

// This object expression implements multiple interfaces.
type IFirst =
  abstract F : unit -> unit
  abstract G : unit -> unit

type ISecond =
  inherit IFirst
  abstract H : unit -> unit
  abstract J : unit -> unit

// This object expression implements an interface chain.
let Implementer() = { new ISecond with
                         member this.H() = ()
                         member this.J() = ()
                       interface IFirst with
                         member this.F() = ()
                         member this.G() = ()
                    }


You use object expressions when you want to avoid the extra code and overhead that is required to create a new, named type. If you use object expressions to minimize the number of types created in a program, you can reduce the number of lines of code and prevent the unnecessary proliferation of types. Instead of creating many types just to handle specific situations, you can use an object expression that customizes an existing type or provides an appropriate implementation of an interface for the specific case at hand.

Community Additions

ADD
Show:
© 2014 Microsoft