[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

OOP in Scheme (addendum)



; methods.scm  (ADDENDUM)
;
; ===============================================================
; Brian Beckman                  |    brian@topaz.jpl.nasa.gov
; Mail Stop 510-202              |    (818) 397-9207
; Jet Propulsion Laboratory      | 
; Pasadena, CA 91109             |    30 June 1989
; ===============================================================

; There are two ways to invoke a method in an object.  The
; first is to send the object a message, getting back a procedure.
; This procedure can then be invoked at will on an appropriate
; set of arguments.  Such an idiom usually results in expressions
; like the following:
;
;   ((foo 'do-it) arg1 arg2)
;
; This is fairly readable and a fine idiom, but it has its 
; limitations.  Suppose that this expression were to result in
; another object, to which we should like to send the message
; 'baz with the arguments 'rat and 'ter.  Then we should write
; the following:
;
;   ((((foo 'do-it) arg1 arg2) 'baz) 'rat 'ter)
;
; The number of leading parentheses is a problem.  It is easy 
; to devise nested message passing expressions that are
; much more difficult to write than to devise, merely because of
; the number of leading parentheses that must be presaged.  Lisp 
; already has a problem with closing parentheses; we don't want
; to compound the felony in this package by introducing a 
; corresponding problem with opening parentheses.  
;
; We need a ``send'' routine that does little more than reduce
; the need for leading parenthese.  This is, admittedly, merely 
; a syntactic issue.  Consider the following, which is the second
; way to send a message to an object:

(define (send object msg . args)
  (apply (object msg) args))

; The earlier example message passing expressions can now be
; much more easly written much more nicely, as follows:
;
;    (send object 'msg arg1 arg2)
;
; and
;
;    (send (send object 'msg arg1 arg2) 'baz 'rat 'ter)