Fsharp: Struct

By Xah Lee. Date: .

fsharp struct

what's struct in F Sharp

(ai answer)

Struct in F# is a value type for small, frequently copied data. Use the struct keyword or [<Struct>] attribute.

type Point =
    struct
        val X: float
        val Y: float
        new(x, y) = { X = x; Y = y }
    end

Stored on the stack (or inline), passed by value. More efficient than classes for simple aggregates.

how to explain struct in a formal way without referring to stack because that relates to implementation

A struct in F# is a value type that groups a small collection of related fields (and optional members) into a single unit with value semantics.

Assignment, parameter passing, and returns copy the entire contents. Two struct values are equal when their fields are equal; they have no independent identity.

Use structs for compact, frequently copied data whose meaning is fully determined by their contents.