common lisp和emacs lisp之间的结构差异

common lisp和emacs lisp之间的结构差异,emacs,lisp,elisp,Emacs,Lisp,Elisp,下面的代码在common lisp中工作,但在emacs lisp中,它会抱怨“(错误“方法参数中的未知类类型orc”)”。为什么以及如何在emacs lisp中修复它?谢谢 (defun randval (n) (1+ (random (max 1 n)))) (defstruct monster (health (randval 10))) (defstruct (orc (:include monster)) (club-level (randval 8))) (defmetho

下面的代码在common lisp中工作,但在emacs lisp中,它会抱怨“(错误“方法参数中的未知类类型orc”)”。为什么以及如何在emacs lisp中修复它?谢谢

(defun randval (n)
  (1+ (random (max 1 n))))

(defstruct monster (health (randval 10)))

(defstruct (orc (:include monster)) (club-level (randval 8)))

(defmethod monster-show ((m orc))
  (princ "A wicked orc with a level ")
  (princ (orc-club-level m))
  (princ " club"))

问题是。。。defmethod需要它是一个类,而不是一个结构,eLisp中的结构只是向量。也许你可以想出你自己的通用分派方法,但也许仅仅使用类而不是结构就能解决这个问题——类是在eio.el中实现的,所以你可以看看它的内部,看看它们是如何进行分派的。或者你可以简单地做一些类似的事情:

(defun foo (monster)
  (cond
    ((eql (aref monster 0) 'cl-orc-struct) ...) ; this is an orc
    ((eql (aref mosnter 0) 'cl-elf-struct) ...) ; this is an elf
    (t (error "Not a mythological creature"))))
这实际上取决于有多少类生物,也许你可以想出一些宏来隐藏条件,或者根据类型标记返回要调用的函数等等

下面是一个制作自己的泛型的简化想法,以防您想要坚持使用结构,并且不需要太多功能或乐于自己实现它:

(defvar *struct-dispatch-table* (make-hash-table))

(defun store-stuct-method (tag method definition)
  (let ((sub-hash
     (or (gethash method *struct-dispatch-table*)
         (setf (gethash method *struct-dispatch-table*)
           (make-hash-table)))))
    (setf (gethash tag sub-hash) definition)))

(defun retrieve-struct-method (tag method)
  (gethash tag (gethash method *struct-dispatch-table*)))

(defmacro define-struct-generic (tag name arguments)
  (let ((argvals (cons (caar arguments) (cdr arguments))))
    `(defun ,name ,argvals
       (funcall (retrieve-struct-method ',tag ',name) ,@argvals))))

(defmacro define-struct-method (name arguments &rest body)
  (let* ((tag (cadar arguments))
     (argvals (cons (caar arguments) (cdr arguments)))
     (generic))
    (if (fboundp name) (setq generic name)
      (setq generic 
        `(define-struct-generic 
          ,tag ,name ,arguments)))
    (store-stuct-method 
     tag name 
     `(lambda ,argvals ,@body)) generic))

(define-struct-method test-method ((a b) c d)
  (message "%s, %d" a (+ c d)))

(test-method 'b 2 3)
"b, 5"

谢谢,我会在课堂上检查的。