Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-dvmhs Total loading time: 0 Render date: 2024-09-28T05:18:25.683Z Has data issue: false hasContentIssue false

26 - Classes and Methods

from Part XI - Dynamic Dispatch

Published online by Cambridge University Press:  05 March 2016

Robert Harper
Affiliation:
Carnegie Mellon University, Pennsylvania
Get access

Summary

It often arises that the values of a type are partitioned into a variety of classes, each classifying data with distinct internal structure. A simple example is provided by the type of points in the plane, which are classified according to whether they are represented in cartesian or polar form. Both are represented by a pair of real numbers, but in the cartesian case, these are the x and y coordinates of the point, whereas in the polar case, these are its distance r from the origin and its angle θ with the polar axis. A classified value is an object, or instance, of its class. The class determines the type of the classified data, the instance type of the class; the classified data itself is the instance data of the object.

Methods are functions that act on classified values. The behavior of a method is determined by the class of its argument. The method dispatches on the class of the argument. Because the selection is made at run-time, it is called dynamic dispatch. For example, the squared distance of a point from the origin is calculated differently according to whether the point is represented in cartesian or polar form. In the former case, the required distance is x2 + y2, whereas in the latter it is simply r2. Similarly, the quadrant of a cartesian point can be determined by examining the sign of its x and y coordinates, and the quadrant of a polar point can be calculated by taking the integral part of the angle θ divided by π/2.

Dynamic dispatch is often described in terms of a particular implementation strategy, which we will call the class-based organization. In this organization, each object is represented by a vector of methods specialized to the class of that object. We may equivalently use a method-based organization in which each method branches on the class of an object to determine its behavior. Regardless of the organization used, the fundamental idea is that (a) objects are classified and (b) methods dispatch on the class of an object. The class-based and method-based organizations are interchangeable and, in fact, related by a natural duality between sum and product types.We explain this symmetry by focusing first on the behavior of each method on each object, which is given by a dispatch matrix.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2016

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@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.

  • Classes and Methods
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.028
Available formats
×

Save book 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 Dropbox.

  • Classes and Methods
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.028
Available formats
×

Save book to Google Drive

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.

  • Classes and Methods
  • Robert Harper, Carnegie Mellon University, Pennsylvania
  • Book: Practical Foundations for Programming Languages
  • Online publication: 05 March 2016
  • Chapter DOI: https://doi.org/10.1017/CBO9781316576892.028
Available formats
×