Programming Language for Old Timers

by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.

Previous page   Table of Contents   Next page

Casting And Delegation

The method selection step of function invocation is normally based on the classes of the arguments. Sometimes you want to select a subset of applicable methods so that the least general applicable method will be more general than the method that would normally be dispatched. This is accomplished using the cast operator as.

The expression x as t has any object as its left-hand argument and a type-specifier as its right-hand argument. It signals an error if the object is not a member of the type. The result is the object. As a side-effect, if an expression x as t is used as an argument to a function invocation, the argument type for purposes of method selection is t rather than the class of x.

For example, if square is a subclass of shape:

defun draw(g is graphics, s is square, x is integer, y is integer)
  ;; Do square-specific code
  ;; Delegate the rest to the more general method
  draw(g, s as shape, x, y)

Previous page   Table of Contents   Next page