liveBook · Manning

Object expression

As explained in F# in Action: A Revised Edition of Get Programming with F#

An object expression is an F# expression that creates an object implementing specified class or interface members without requiring a separately declared type.

An object expression creates an interface implementation directly, without first declaring a concrete implementation type. It is useful when an F# application needs an interface for C# consumption, a runtime-pluggable API, or a concise function-based contract. section 11.1.2

The compact form uses `new InterfaceName with` followed by the required members. Compared with a named implementation type, it removes boilerplate while preserving the interface contract. Annotate the value as the interface when member access is otherwise unavailable through the inferred type. section 11.1.2

Implementing an interface inline

let implementation = { new MyInterface with member _.Capitalize text = text.ToUpper() member _.Add number = number + 1 } let text = implementation.Capitalize "test" For a single-method interface, wrap a function instead of writing a named type: let makeIDisplayTime implementation = { new IDisplayTime with member _.Display date = implementation date } let shortPrinter = makeIDisplayTime (fun date -> $"It's {date.ToShortTimeString()}.") section 11.1.2 section 11.1.7

let implementation =
    { new MyInterface with                             #1
        member this.Capitalize text = text.ToUpper()   #2
        member _.Add number = number + 1               #3
    }
 
let text = implementation.Capitalize "test"            #4

Use the complete inline implementation to see how object expressions replace a separately declared implementation type. section 11.1.2

let makeIDisplayTime implementation =                 #1
    { new IDisplayTime with
        member _.Display date = implementation date   #2
    }

Use the function-to-interface helper to connect functional implementations with interfaces expected by C# applications. section 11.1.7

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Defines syntax and application patterns, but stops short of broader advanced alternatives such as active patterns and statically resolved type parameters.
Examples
Demonstrates multi-member interfaces, time-display adapters, and selectable printer implementations.
What it assumes
Assumes familiarity with F# member syntax and interface declarations while demonstrating the inline construction pattern.
What you can do after
Build it from scratch by mapping functions into interface instances without declaring concrete implementation types.
How it connects
Connects interfaces with C#, records, discriminated unions, functions, and abstract factories.

Also explained in

Compare how 2 books explain object expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.