(defun create-methods (classname methods)
(mapcar
(lambda (method)
- (eval (list* 'defun
- (first method)
- (append '(inst) (second method))
- (append '((unless (is-instance inst)
- (error "~A non è un'istanza" inst)))
- (cddr method)))))
+ (setf (symbol-function (first method))
+ (lambda (this &rest args)
+ (unless (is-instance this)
+ (error "~A non è un'istanza" this))
+ (unless (get-method (getf this :methods) (first method))
+ (error "~A non ha un metodo ~A" (getf this :classname) (first method)))
+ (unless (eq (length args)
+ (length (second (get-method (getf this :methods) (first method)))))
+ (error "~A numero di argomenti errato" (first method)))
+ (cddr (get-method (getf this :methods) (first method))))))
methods))
+
+(defun get-method (methods name)
+ (cond ((null methods) nil)
+ ((eq (caar methods) name) (car methods))
+ (t (get-method (cdr methods) name))))
#+end_src
** Creazione di un'istanza
(defun create-methods (classname methods)
(mapcar
(lambda (method)
- (eval (list* 'defun
- (first method)
- (append '(inst) (second method))
- (append '((unless (is-instance inst)
- (error "~A non è un'istanza" inst)))
- (cddr method)))))
+ (setf (symbol-function (first method))
+ (lambda (this &rest args)
+ (unless (is-instance this)
+ (error "~A non è un'istanza" this))
+ (unless (get-method (getf this :methods) (first method))
+ (error "~A non ha un metodo ~A" (getf this :classname) (first method)))
+ (unless (eq (length args)
+ (length (second (get-method (getf this :methods) (first method)))))
+ (error "~A numero di argomenti errato" (first method)))
+ (cddr (get-method (getf this :methods) (first method))))))
methods))
+(defun get-method (methods name)
+ (cond ((null methods) nil)
+ ((eq (caar methods) name) (car methods))
+ (t (get-method (cdr methods) name))))
+
(defun create-instance (listclass new-fields)
;; Se c'è almeno un field da cambiare
;; il cui nome non è una proprietà della classe