小编Pat*_*ick的帖子

程序功能定义:如何摆脱"eval"?

我有一组名为"ip","date","url"等的函数.

有了这些,我想生成另一组函数"ip-is","date-is"等.

我终于得到了以下解决方案,即工作正常,但使用"eval".

(loop for name in '(ip date url code bytes referer user-agent) do
  (let ((c-name (intern (concatenate 'string (symbol-name name) "-IS"))))
    (eval `(defun ,c-name (c)
           #'(lambda (l) (equal (,name l) c))))))
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我,如何摆脱"邪恶的评估"?我的程序必须将函数名称作为列表提供.所以打电话给一些marcro

   (define-predicate ip)
   (define-predicate date)
   (define-predicate url)
Run Code Online (Sandbox Code Playgroud)

等等

不符合我的需要.我对"eval"没有任何实际问题,但我经常阅读,eval被认为是不好的风格,如果可能的话应该避免.

提前致谢!

common-lisp

5
推荐指数
2
解决办法
160
查看次数

sbcl/CLOS为什么我必须在这里添加"validate-superclass"-Method?

在SBCL中,当我定义新的元类时

CL-USER> (defclass counting-class (standard-class)
   ((counter :initform 0)))
#<STANDARD-CLASS COUNTING-CLASS>
Run Code Online (Sandbox Code Playgroud)

并向GF"make-instance"添加一个方法:

CL-USER> (defmethod make-instance :after ((class counting-class) &key)
   (incf (slot-value class 'counter)))
#<STANDARD-METHOD MAKE-INSTANCE :AFTER (COUNTING-CLASS) {25302219}>
Run Code Online (Sandbox Code Playgroud)

如果我尝试创建实例,则会收到错误:

CL-USER> (defclass counted-point () (x y) (:metaclass counting-class))

The class #<STANDARD-CLASS STANDARD-OBJECT> was specified as a
super-class of the class #<COUNTING-CLASS COUNTED-POINT>, but
the meta-classes #<STANDARD-CLASS STANDARD-CLASS> and
#<STANDARD-CLASS COUNTING-CLASS> are incompatible.  Define a
method for SB-MOP:VALIDATE-SUPERCLASS to avoid this error.
Run Code Online (Sandbox Code Playgroud)

现在,如果我添加所需的定义:

CL-USER>  (defmethod sb-mop:validate-superclass ((class counting-class)
                                                 (super standard-class))
            t)
#<STANDARD-METHOD SB-MOP:VALIDATE-SUPERCLASS …
Run Code Online (Sandbox Code Playgroud)

common-lisp clos mop

2
推荐指数
1
解决办法
537
查看次数

标签 统计

common-lisp ×2

clos ×1

mop ×1