(lambda (this &rest args) ;; Adds The THIS Argument
(apply (get-slot this
method-name)
(append (list this)
args))))
重写方法
;;;; This Function Add The Argument This To The Method
(defun rewrite-method (method-spec)
(list 'lambda (append (list 'this) ;; Add The THIS Argument
(second method-spec))
(cons 'progn (rest (rest method-spec))))) ;; Eval All The Method's Body
;;;; Method-Process
;;;; Function Used To Process Methods, Rewriting Them To
;;;; Lisp Functions
(defun method-process (method-name method-spec)
(setf (fdefinition method-name) ;; Associate The Lambda To The Method's Name
(lambda (this &rest args) ;; Adds The THIS Argument
(apply (get-slot this
method-name)
(append (list this)
args))))
(eval (rewrite-method method-spec))) ;; Returned Value
答案 0 :(得分:3)
需要一个arglist (foo bar baz)
并添加this
参数:(this foo bar baz)
。
因此,函数(lambda (foo bar baz) ...)
为(lambda (this foo bar baz) ...)
。
这对于单继承面向对象语言的实现非常有用,其中第一个参数是接收消息和其他参数的对象。这里参数this
是接收对象。
请注意,可以改进对APPLY
的调用。 (apply function (append (list this) args))
只是(apply function this args)
。