Previous: , Up: Records   [Contents]

2.5 Record Polymorphism

Record polymorphism is achieved through subtyping: Two records R and S are compatible if the slot count #S#R. When S is compatible with R, then S may be declared a subtype of R; R  is referred to as the supertype of S. No record may have more than one supertype, but any record may have more than one subtype. Any subtype T of S is also a subtype of R.

Compatibility ensures that, when a record S is provided in place of another record R, that the callee is able to extract at least the amount of information that it expects to be available from R. Explicit subtyping declares intent —it provides context that is important API documentation and helps to catch logic errors. For example, even though a record named Withdrawl and Deposit may both have two slot values (representing a monetary value for a bank transaction on a given account), their intents are vastly different, and using one where another should be used indicates a logic error: the system began initiating a withdrawl but invoked a deposit function, for example.