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"))
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:
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: