On Wed, Jun 10, 2009 at 10:42 AM, Francesco
Petrogalli<[email protected]> wrote:
> Hi,
> I woud like to create new specializer for my object system:
>
> (defclass shift ()
>  ((i :initarg :i :reader shift-i)
>   (j :initarg :j :reader shift-j))
>  (:documentation "Couple of numbers"))
>
> (defmethod create-shift ((i integer) (j integer))
>  (make-instance 'shift :i i :j j))
>
> The problem is quite simple. I want to define a method "top-exists"
> that returns false if slot i is equal to zero, true otherwise.
>
> I've tried this as a special case, it works:
> (defmethod top-exists ((sh (eql (create-shift 0 0)))) nil)
> (top-exists (create-shift 0 0 )) ==> nil
>
> I know I can simply use this to solve my problems:
> (defmethod top-exists ((sh shift))
>  (> (shift-i sh) 0))
>
> But I would like it to be smarter, something like this:
> (defmethod top-exists ((sh (slot-i-is-equal-to 0))) nil)
>
> Is it possible?

As far as I know, the CLOS MOP allows custom specializers to be used.
However, 1) the MOP is not standard (though many implementations
include it; you can use Pascal Costanza's Closer to MOP library to
smooth out implementation differencies). 2), I don't think DEFMETHOD
can be used with your custom specializers: you'll probably have to
call lower-level functions (like ensure-method) to manually build the
method metaobject and register it with the generic function.

> Thanks,
>
> Francesco
>
> PS I defined the method "create-shift" for i and j integer because I
> dont' know how to require the type of the slots of a class. In my case
> I want i and j to be positive integers or zero. Is there a way to do
> this in defclass? :)

You can add an :after method on initialize-instance to do additional
checks. E.g.

(defmethod initialize-instance :after ((sh shift) &rest args) ;I
believe the signature is this, but not sure...
  (check-type (slot-x-of-shift sh) (integer 0 T)))

Ciao,
Alessio
_______________________________________________
Gardeners mailing list
[email protected]
http://www.lispniks.com/mailman/listinfo/gardeners

Reply via email to