Ch.7 SML# feature: record polymorphism

§ 7.4. Functional record update

SML# contains functional record update expressions, whose syntax is given below.

expr  ::=
|expr # {l1=expr1,, ln=exprn}

This expression creates a new record by modifying the value of each label li to expri. This is an expression to create a new record; the original record expr is not mutated. This expression has a polymorphic type according to the ML's principle of most general typing. Below is an example using this expression.

# fun f modify x = x # {X=3};
val f = _ : ['a#{X:'b}, 'b. 'a -> 'a]

The following is a useful idiom in record programming.

# fun reStructure (p as {Salary,...}) = p # {Salary=Salary * (1.0 - 0.0803)};
val f = _ : ['a#{Salary:real}. 'a -> 'a]

Function reStructure takes an employee record p and reduces its Salary field by 8.03%. As seen in its typing, this function can be applied to any record as far as it contains a ,Salary field.