Type Declarations

Q# supports user-defined struct types. struct types are similar to record types in F#; they are immutable but support a copy-and-update construct.

Struct types

struct types can only contain named items and do not support anonymous items. Any combination of named items is supported, although items cannot be nested.

The following declaration, for example, defines a struct Complex which has two named items Real and Imaginary, both of type Double:

struct Complex {
    Real : Double,
    Imaginary : Double,
}

You can access the contained items via their name or by deconstruction (for more information, see item access). You can also access a tuple of all items where the shape matches the one defined in the declaration via the unwrap operator.

struct types are useful for two reasons. First, as long as the libraries and programs that use the defined types access items via their name rather than by deconstruction, the type can be extended to contain additional items later on without breaking any library code. Because of this, accessing items via deconstruction is generally discouraged.

Second, Q# allows you to convey the intent and expectations for a specific data type since there is no automatic conversion between values of two struct types, even if their item types are identical.

Struct constructors

Constructors for new struct types are automatically generated by the compiler when it reads a struct definition. For the Complex struct in the previous example, you can create an instance with

let complexPair = Complex(1.4, 2.1);

Instances can also be defined by the user with the new keyword, for example

let complexPair = new Complex { Real = 1.4, Imaginary = 2.1 };

For more information about copying and updating structs, see Copy and update of struct types.