这是sbcl中的错误吗? [英] This is a bug in sbcl?

查看:93
本文介绍了这是sbcl中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在sbcl中发生这种情况?也许是个错误?

Why happen this in sbcl? Maybe a bug?

(defclass myclass ()
  ((s1
    :initform '((a . 1) (b . 2))) 
   (s2
    :initform '((a . 1) (b . 2)))))

(defparameter ins (make-instance 'myclass))

(setf (cdr (assoc 'a (slot-value ins 's1))) 43) ;; change only slot s1

;; here my problem

(slot-value ins 's1)  ;; => ((a . 44) (b . 2)))
(slot-value ins 's2)  ;; => ((a . 44) (b . 2)))

但是如果将:initform更改为:

But if change :initform to :

(defclass myclass ()
  ((s1
    :initform '((a . 1) (b . 2))) 
   (s2
    :initform '((a . 1) (b . 3)))))

问题消失了

我在 sbcl 1.4.3 1.4.11 中对此进行了测试.似乎没有出现问题.

I test this in sbcl 1.4.3 and 1.4.11. In clisp it seems that the problem does not arise.

推荐答案

否.您正在修改文字数据,这会带来不确定的后果.

No. You are modifying literal data, which has undefined consequences.

在源代码中引用列表时,这意味着您要使用的东西正是阅读器从源代码生成的列表,这是文字列表.读者可能会记住这些事情,因此不会重复两个相同的列表.

When you quote a list in source code, it means that the thing you want to work with is exactly the list that the reader produced from your source code—this is a literal list. Such things may be remembered by the reader so that two identical lists are not duplicated.

解决此问题的一种方法是在运行时使用listcons创建列表:

One way to fix this is to create the lists at runtime, using list and cons:

(defclass myclass ()
  ((s1
    :initform (list (cons a 1) (cons b 2))) 
   (s2
    :initform (list (cons a 1) (cons b 2)))))

这篇关于这是sbcl中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆