Ch.7 SML# feature: record polymorphism

§ 7.2. Field selection operation

The basic operation on records is to select a field value by specifying a field label. For this, the following primitive function is defined for any label l.


This function takes a record containing a field labeled with l, and returns the value of that label. According to the ML principle, this function should be applicable to any record containing label l. For this primitive, SML# infers the following polymorphic typing.

# #X;
val it = _ : ['a#{X:'b},'b. 'a -> 'b]

The notation 'a#{X:'b} is a type variable 'a which represents an arbitrary record type that contains a field labeled X of type 'b. The inferred type is a most general type of #X. Below show some examples.

# #X {X=1.1, Y=2.2};
val it = 1.1 : real
# #X {X = 1, Y = 2, Z = 3};
val it = 1 : int

Functions using these operations are polymorphic in record structures, as seen in the following example.

# fun f x = (#X x, #y y);
val f = _ : ['a#{X:'b,Y:ć},'b, 'c. 'a -> 'b * 'c]

The type of f indicates that this is a function that takes any record containing X:'b and Y:'c fields and returns a pair of type 'b * 'c. This is a most general polymorphic type of this function. So this function can be freely combined with other expressions as far the combination is type consistent.