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

Re: Lexical environment of method

	From att!AI-SUN.jpl.nasa.gov!Charest Fri Mar  9 14:59 PST 1990
	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

Try this:
(defflavor widget ((fn) (ivar "puzzle")) () :writable-instance-variables)
(setq foo (make-instance 'widget))
(setf (widget-fn foo) (compile nil #'(lambda (string) (format nil 
     "~a ~a" string (widget-ivar foo)))

	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
	(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.
		Len Charest, JPL AI Lab
These opinions are shareware.  If you like the product,
please send your $0.02 to
               David Loewenstern
   {backbone!}att!whutt!davel which is davel@whutt.att.com