下面的代码在普通的lisp中工作,但在emacs lisp中,它会报告"(error "Unknown class type orc in method parameters")“。为什么以及如何在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"))发布于 2012-06-10 20:33:05
问题是..。defmethod需要它是一个类,而不是一个结构,eLisp中的结构只是一个向量。也许你可以想出你自己的通用分派方法,但是仅仅使用类而不是结构就可以解决这个问题--类是用eieio.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"https://stackoverflow.com/questions/10967372
复制相似问题