Lisp方法过程

时间:2015-01-23 11:01:00

标签: lisp common-lisp

你能帮帮我吗?我不明白这段代码的部分

(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

1 个答案:

答案 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)