From: frito
Subject: CLOS and Macros
Date: 
Message-ID: <1163449888.373547.136990@m73g2000cwd.googlegroups.com>
Hi,

I am trying to do the following but get the error in the comments.

(defclass something()
  ((slot1 :initarg :slot1 :accessor slot1)))

(defmacro def-class (c)
     (let ((slot1-name (slot1 c)))
	   `(list  slot1-name)))

(setf m (make-instance 'something :slot1 'bob))

(def-class m)

;There is no applicable method for the generic function
;  #<STANDARD-GENERIC-FUNCTION SLOT1 (1)>
;when called with arguments
;  (M).

Any ideas welcome,
Best wishes,
Keith

From: Ken Tilton
Subject: Re: CLOS and Macros
Date: 
Message-ID: <iZ46h.1387$6B3.1194@newsfe08.lga>
frito wrote:
> Hi,
> 
> I am trying to do the following but get the error in the comments.
> 
> (defclass something()
>   ((slot1 :initarg :slot1 :accessor slot1)))
> 
> (defmacro def-class (c)
>      (let ((slot1-name (slot1 c)))
> 	   `(list  slot1-name)))
> 
> (setf m (make-instance 'something :slot1 'bob))
> 
> (def-class m)
> 
> ;There is no applicable method for the generic function
> ;  #<STANDARD-GENERIC-FUNCTION SLOT1 (1)>
> ;when called with arguments
> ;  (M).

Turn off Usenet, engage brain. Hint: Lisp error messages are more useful 
than in other languages. Your macro is being passed M. If you do not 
believe that, try (print (list cc (type-of c)) unquoted as the first 
line of your macro. And prepare for the light to come on.

hth, kenny

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: Pascal Bourguignon
Subject: Re: CLOS and Macros
Date: 
Message-ID: <87psbrgj47.fsf@thalassa.informatimago.com>
"frito" <············@gmail.com> writes:

> Hi,
>
> I am trying to do the following but get the error in the comments.
>
> (defclass something()
>   ((slot1 :initarg :slot1 :accessor slot1)))
>
> (defmacro def-class (c)
>      (let ((slot1-name (slot1 c)))
> 	   `(list  slot1-name)))
>
> (setf m (make-instance 'something :slot1 'bob))
>
> (def-class m)
>
> ;There is no applicable method for the generic function
> ;  #<STANDARD-GENERIC-FUNCTION SLOT1 (1)>
> ;when called with arguments
> ;  (M).

Contrarily to functions, macros receives their arguments _unevaluated_.


(defun f (c) (print c))

(f (+ 1 2))  ; f will only see 3, the result of the evaluation of (+ 1 2)
             ; and therefore, f will print 3.

(defmacro m (c) (print c) '(list 'hi))

(m (+ 1 2))  ; m receives the arguments _unevaluated_, that is, literal
             ; form (+ 1 2), and it will print (at macro-expansion time)
             ; the form (+ 1 2) itself. Then it will return the  form
             ; (list (quote hi)) and this form will be compiled to be 
             ; evaluated at run time. Eventually, (m (+ 1 2)) 
             ; returns the list: (hi).


(macroexpand '(m (+ 1 2)))
prints:
(+ 1 2) 
returns:
(LIST 'HI) ;
T


For you def-class macro, since you intend to pass it a _value_,
instead of a _variable_ or _place_, you'd better make it a
function. Since this function returns a slot of the object value,
you'd better name it get-my-slot or something like that:

(defun get-my-slot (object)
   (slot1 object))

(get-my-slot m) --> BOB


Or:

(defun get-my-slot (object)
   (list (slot1 object)))

if you want:

(get-my-slot m) --> (BOB)



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.