Published online by Cambridge University Press: 29 May 2025
Subtyping is a preorder on types that can be thought of as inclusion between the sets of the values of the types. If S is a subtype of T then a value of type S is a also a value of type T. For instance if Nat, Int, and Real are the types of natural numbers, integers and reals, respectively, then we could naturally stipulate that Nat is a subtype of Int and that Int is a subtype of Real.
If S is a subtype of T then an expression of type S can always replace an expression of type T; if the program before the replacement is well typed, then also the program after the replacement will be so. This is so because the operations available on values of type T - both the pre-defined and the user-defined operations - are also available on values of type S. For instance, operations such as multiplication and division that are available on Real are also available on Int. The converse, by contrast, may be false; for instance on Int there are operations like successor and predecessor that do not make sense on Real. The possibility of having operations that work on all subtypes of a given type is a major advantage of subtyping in a programming language. (In the implementation of programming languages, the subtyping from integers to reals requires a conversion that modifies the representation of a number; therefore operations defined on both types will have different implementations for the two types. Due to this non-uniformity, the subtyping between integers and reals is called ‘ad hoc’ subtyping. In general, all subtyping between basic types is ad hoc. The subtyping on structured types - the non-basic types - that will be presented in this section is, by contrast, uniform.)
Programming language constructs that are usually associated with subtyping are records, variants, and objects. Subtyping is a key feature of object-oriented languages (perhaps the key feature). Indeed, the study of subtyping and of the associated programming constructs has been motivated mainly by the interest in object-oriented languages.
To save this book to your Kindle, first ensure no-reply@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.