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

Flavor-relative Functions



I am trying to define some functions whose definition changes as a 
function of the flavor class they are called inside of. These functions
will be called by methods, and should have access to the flavor's
instance variables. For example, given the following:

(defflavor f1 (v1) ()
  :special-instance-variables
  (:special-instance-variable-binding-methods meth))

(defmethod (meth f1) ()
  ... (func1 <arg>* ) ... )

(defflavor f2 () ())

I would like func1 to have a different def'n if called from inside an
instance of flavor f1 vs. an instance of flavor f2.

The straightforward implementation, of course, would appear to be to
define func1 as a method. Unfortunately, in my code the instance
variables get "reset" when a method is called by another method. To
give an example of what I mean:

(defflavor foo (v1 10) ()
   :special-instance-variables
   (:special-instance-variable-binding-methods inner outer))

(defmethod (outer foo) () (print v1) (incf v1) (inner foo))

(defmethod (inner foo) () (print v1))

(setq instance (make-instance 'foo))

(outer instance)
=> 10 ; the original value of v1, in call to outer
=> 10 ; the value for v1 printed in the call to inner - should be 11!?

If one actually runs the simple example just described, the call to
inner will print 11, not 10 - it thus works correctly. My more
complicated code, however, does produce behavior just as described.

What could I be doing wrong? What should I be doing differently?

An option to methods, of course, is to simply define function-valued
instance variables whose value differs from instance to instance. This
is undesirable for two reasons, though:

(1) I only need the function definitions to change by class, not by
    instance

(2) I have to call these functions using apply or funcall, as opposed
    to simply by name, which seems needlessly confusing to me.

Admittedly these considerations are somewhat aesthetic in nature, but
I feel like there should be a straightforward way to do what I want
without the ugliness of function-valued instance variables.

Sorry if I am missing something obvious, but I have struggled with
this for a few days and have made no progress.

Thanks in advance for any help!

		Mark Klein, Ph.D.
		Hitachi Ltd.
		Advanced Research Lab
		Higashi Koigakubo 1-280
		Kokubunji, Tokyo 185

		mklein@harl.hitachi.co.jp
		0426-23-1111 ext. 2822