Reputation: 2915
The code below works in common lisp, but in emacs lisp, it complains "(error "Unknown class type orc in method parameters")". Why and how can i fix it in emacs lisp? Thanks.
(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"))
Upvotes: 5
Views: 886
Reputation:
The thing is... defmethod needs it to be a class, not a struct, structs in eLisp are just vectors. Maybe you could come up with your own generic dispatch method, but probably just using classes instead of structs will solve it - classes are implemented in eieio.el, so you may look at the insides of it and see how they do dispatching. Or you could simply have it something like:
(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"))))
It would really depend on how many classes of creatures are there, probably you could come up with some macro that hides the condition or rather returns the function to call based on the type tag etc.
Below is a simplified idea for making your own generics, just in case you want to stick with the structs and you don't require a lot of functionality or are happy to implement it on your own:
(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"
Upvotes: 2