let Bindings in Classes

You can define private fields and private functions for F# classes by using let bindings in the class definition.

Syntax

// Field.
[static] let [ mutable ] binding1 [ and ... binding-n ]

// Function.
[static] let [ rec ] binding1 [ and ... binding-n ]

Remarks

The previous syntax appears after the class heading and inheritance declarations but before any member definitions. The syntax is like that of let bindings outside of classes, but the names defined in a class have a scope that is limited to the class. A let binding creates a private field or function; to expose data or functions publicly, declare a property or a member method.

A let binding that is not static is called an instance let binding. Instance let bindings execute when objects are created. Static let bindings are part of the static initializer for the class, which is guaranteed to execute before the type is first used.

The code within instance let bindings can use the primary constructor's parameters.

Attributes and accessibility modifiers are not permitted on let bindings in classes.

The following code examples illustrate several types of let bindings in classes.

type PointWithCounter(a: int, b: int) =
    // A variable i.
    let mutable i = 0

    // A let binding that uses a pattern.
    let (x, y) = (a, b)

    // A private function binding.
    let privateFunction x y = x * x + 2 * y

    // A static let binding.
    static let mutable count = 0

    // A do binding.
    do count <- count + 1

    member this.Prop1 = x
    member this.Prop2 = y
    member this.CreatedCount = count
    member this.FunctionValue = privateFunction x y

let point1 = PointWithCounter(10, 52)

printfn "%d %d %d %d" (point1.Prop1) (point1.Prop2) (point1.CreatedCount) (point1.FunctionValue)

The output is as follows.

10 52 1 204

Alternative Ways to Create Fields

You can also use the val keyword to create a private field. When using the val keyword, the field is not given a value when the object is created, but instead is initialized with a default value. For more information, see Explicit Fields: The val Keyword.

You can also define private fields in a class by using a member definition and adding the keyword private to the definition. This can be useful if you expect to change the accessibility of a member without rewriting your code. For more information, see Access Control.

See also