8.1 Record expressions
The syntax of record expressions is given below.
{=,, =} |
denotes a string called labels. Below is a simple record expression.
# val point = {X =0.0, Y=0.0};
val point = {X = 0.0, Y = 0.0} : {X: real, Y: real}
A record whose labels are consecutive numbers starting with 1 is interpreted as a tuple and printed specially.
# {1 = 1.1, 2 = fn x => x + 1, 3 = "SML#"};
val it = (1.1,fn,"SML#") : real * (int -> int) * string
# (1,2);
val it = (1,2) : int * int
In Section 7.8, we defined a multiple-argument function, namely powerUncurry (n,C). This is a function that takes a tuple.
Just like lists, record elements can contain values of any types. A record forming function is therefore polymorphic, as seen in the following example.
# fun f x y = {X = x, Y = y};
val f = fn : [’a. ’a -> [’b. ’b -> {X: ’a, Y: ’b}]]
# fun g x y = (x, y);
val g = fn : [’a. ’a -> [’b. ’b -> ’a * ’b]]