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

Lexical environment of method



I want to write an arbitrary function to be called inside the body of a method and I want the function to receive the
lexical environment of the method. The function is to be compiled and stored on an instance variable of a flavor. The
method will take an arbitrary arglist and simply forward it to the function. For example: 

(defflavor widget ((fn) (ivar "puzzle")) ())
	
(setq foo (make-instance 'widget :fn (compile nil '(lambda (string) (format t "~%~a ~a" string ivar)))))
;;;see what is stored on fn
(widget-fn foo) => #<dtp-compiled-function ...>
	
(defmethod (doit widget) (&optional &rest args)
  ;;fn knows about the lexical environment of this method
  (apply fn args))

;;;call the doit method
(doit foo "Jigsaw") =>
Jigsaw puzzle

The motivation for the above is to essentially have methods that are private to a specific instance rather than available to
an entire flavor (class).
Alternatively, I could easily create in-flavor functions and pray that no two instances used the same function name. For
example:

(setq foo (make-instance 'widget :fn (defun-in-flavor (foo-fn widget) (string) (format t "~%~a ~a" string ivar))))
;;;see what is stored on fn
(widget-fn foo) => (defun-in-flavor foo-fn widget)

Notice that the function spec for defun-in-flavor is a list. Now how do I get the *function object* to be passed to apply??
That is,
	(apply (widget-fn foo) args) 
will not work.

	Thanx,
	Len Charest, JPL AI Lab