SML# Document Version 4.0.0
8 SML# feature: record polymorphism

8.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 = modify # {X = x};
val f = fn : [’a#{X: ’b}, ’b. ’a -> ’b -> ’a]

The following is a useful idiom in record programming.

# fun reStructure (p as {Salary,...}) = p # {Salary = Salary * (1.0 - 0.0803)};
val reStructure = fn : [’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.