Mutable record fields

Other topics

Declaring a record with mutable fields

In the following, weight is declared as a mutable field.

type person = {
  name: string;
  mutable weight: int
};;

Remark: As far as design is concerned here, one would consider the fact that a person's name isn't likely to change, but their weight is.

Initializing a record with mutable fields

Initializing a record with mutable fields isn't different from a regular record initialization.

let john = { name = "John"; weight = 115 };;

Setting the value to a mutable field

To assign a new value to a mutable record field, use the <- operator.

john.weight <- 120;;

Note: The previous expression has a unit type.

Contributors

Topic Id: 9367

Example Ids: 29014,29015,29016

This site is not affiliated with any of the contributors.