{f1 = e1; f2 = e2} is a record with fields named f1 and f2.e.f accesses field f of a record expression e.f must be an identifier.Evaluation:
ei ==> vi then{f1 = e1; ...; fn = en} => {f1 = v1; ...; fn = vn}.e ==> {..., f = v; ...} thene.f ==> vType checking:
ei : ti and if t is defined to be {f1 : t1, ..., fn : tn} then{f1 = e1; ...; fn = en} : t.e : t and t is defined to be {..., f : tf; ...} thene.f : tf.{e with f1 = e1} will create a copy of record e with field f1 set to e1.
type student = { name: string; year: int; };;
let rbg : student = { name = "Ruth Bader"; year = 1954 };;
type student = { name : string; year : int; }
val rbg : student = {name = "Ruth Bader"; year = 1954}
{rbg with name = "Ruth Bader Ginsburg"}
- : student = {name = "Ruth Bader Ginsburg"; year = 1954}
Note that this created a new record, it's immutable, as always.
rbg;;
- : student = {name = "Ruth Bader"; year = 1954}
In reality, record copy is syntactic sugar for just rewriting the record from scratch. It's much easier to change fields like this. Note that you can't add more fields, because that would change the type:
{rbg with profession = "Justice"};;
File "[4]", line 1, characters 10-20:
1 | {rbg with profession = "Justice"};;
^^^^^^^^^^
Error: This record expression is expected to have type student
The field profession does not belong to type student