Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CLOS and Macros

32 views
Skip to first unread message

frito

unread,
Nov 13, 2006, 3:31:28 PM11/13/06
to
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

Ken Tilton

unread,
Nov 13, 2006, 3:55:42 PM11/13/06
to

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

Pascal Bourguignon

unread,
Nov 13, 2006, 3:57:28 PM11/13/06
to
"frito" <keithm...@gmail.com> writes:

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.

0 new messages