From: Georges KO
Subject: [CLOS] Individual methods on specifc instances
Date: 
Message-ID: <YHsxOEM+pdVkrp5JvHpeAwZ+54J6@4ax.com>
    Hello,

    I would like to write methods that would be specific to some
instances. In CLOS, there is:

    (defmethod my-method ((object (eql some-form)))
	...)

    where some-form is evaluated only at the method definition. 

    What I would like to do is to be able to specialize on some values
of the object's slots. For example, if the object has slots x, y and
z, I would like to call a specific method if x = 10, y = 11 and y = 12
and a default method for all other values. Do I have a way to do it
like (eql some-form) ? That is, write a method for each specific
case. 

    Of course, I can write a single method and manage the whole stuff
with some associative list or array.
-- 
 Georges KO, Taipei        ···@gko.net        ICQ: #8719684 / Yahoo: gko_net
               Cycle 78, an 16 (Ji-Mao), mois 10 (Yi-Hai), jour 9 (Ren-Shen)

From: Barry Margolin
Subject: Re: [CLOS] Individual methods on specifc instances
Date: 
Message-ID: <HRfY3.18$Nh.225@burlma1-snr2>
In article <····························@4ax.com>,
Georges KO  <···@gko.net> wrote:
>    Hello,
>
>    I would like to write methods that would be specific to some
>instances. In CLOS, there is:
>
>    (defmethod my-method ((object (eql some-form)))
>	...)
>
>    where some-form is evaluated only at the method definition. 
>
>    What I would like to do is to be able to specialize on some values
>of the object's slots. For example, if the object has slots x, y and
>z, I would like to call a specific method if x = 10, y = 11 and y = 12
>and a default method for all other values. Do I have a way to do it
>like (eql some-form) ? That is, write a method for each specific
>case. 

You can't specialize on the contents of slots (well, I suppose it would be
possible to define a new type of generic function using MOP that does this,
but it would be extremely non-trivial), only on the types and identities of
function parameters.  There are a couple of ways you could implement your
goal:

1) Define a subclass that's used when those slots have those particular
values, and specialize on the subtype.  This is easiest to accomplish if
the slots are given their values at creation time and they don't change.
You could define a function that creates the objects, returning an instance
of either the general type or the specific type.  If the slots can be
modified on the fly, you would have to define methods that call
CHANGE-CLASS when necessary.

2) Have the method for the class call another function with the slot values
as arguments, e.g.

(defmethod my-method ((self my-class))
  (my-method-internal self
                      (my-class-x self) (my-class-y self) (my-class-z self)))

;; Method specialized on x/y/z = 10/11/12
(defmethod my-class-internal ((self my-class)
                              (x (eql 10)) (y (eql 11)) (z (eql 12)))
  ...)

;; Method for other x/y/z values
(defmethod my-class-internal ((self my-class) x y z)
  ...)

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Robert Monfera
Subject: Re: [CLOS] Individual methods on specifc instances
Date: 
Message-ID: <38318DB1.F165EEE1@fisec.com>
Barry Margolin wrote:

> (defmethod my-method ((self my-class))
>   (my-method-internal self
>                       (my-class-x self) (my-class-y self) (my-class-z self)))
> 
> ;; Method specialized on x/y/z = 10/11/12
> (defmethod my-class-internal ((self my-class)
>                               (x (eql 10)) (y (eql 11)) (z (eql 12)))
>   ...)
> 
> ;; Method for other x/y/z values
> (defmethod my-class-internal ((self my-class) x y z)
>   ...)

You mean my-method-internal instead of my-class-internal.

Robert
From: Robert Monfera
Subject: Re: [CLOS] Individual methods on specifc instances
Date: 
Message-ID: <38318AB6.C03DEF20@fisec.com>
Georges KO wrote:

>     What I would like to do is to be able to specialize on some values
> of the object's slots. For example, if the object has slots x, y and
> z, I would like to call a specific method if x = 10, y = 11 and y = 12
> and a default method for all other values. Do I have a way to do it
> like (eql some-form) ? That is, write a method for each specific
> case.
> 
>     Of course, I can write a single method and manage the whole stuff
> with some associative list or array.

You could also write a jumpboard method, which only specializes for the
class of your instance, and does nothing more than invokes another
generic function (e.g., named my-method-with-slot-value), with both the
instance and the slot value(s) you want to specialize for.

Maybe there are other options for you:  for example, you may change the
class of an existing instance if you encounter that your slot(s) are of
certain values.

If you give some hints as to what you want to achieve by specializing
for slot contents, people can better help - possibly there is a better
way of doing it.

It would have been nice to retain GENERIC-FLET for purposes like this,
although it would be against the ability to extend on those lexical
GF's.

Robert