这是字面弊端的特定于实现的行为吗? [英] Is this an implementation-specific behavior for literal cons?
问题描述
我正在测试这个有趣的答案中的代码.
CL-USER> (defun literal-cons ()
(let ((cons '(1 . 2)))
(incf (cdr cons))
cons))
; in: DEFUN LITERAL-CONS
; (INCF (CDR CONS))
; --> LET*
; ==>
; (SB-KERNEL:%RPLACD #:CONS1 #:NEW0)
;
; caught WARNING:
; Destructive function SB-KERNEL:%RPLACD called on constant data.
; See also:
; The ANSI Standard, Special Operator QUOTE
; The ANSI Standard, Section 3.2.2.3
;
; compilation unit finished
; caught 1 WARNING condition
LITERAL-CONS
CL-USER> (literal-cons)
(1 . 3)
CL-USER> (literal-cons)
(1 . 3)
CL-USER> (literal-cons)
(1 . 3)
由于行为不同,我想知道SBCL是否已使用上述警告将行为更改为用户认为更可能期望的行为?预期:
As the behavior is not the same, I am wondering if SBCL has used the mentioned warning to change the behavior to something it thinks is more likely expected from the user? Expected:
TEST> (defun literal-cons ()
(let ((cons '(1 . 2)))
(incf (cdr cons))
cons))
LITERAL-CONS
TEST> (literal-cons)
(1 . 3)
TEST> (literal-cons)
(1 . 4)
TEST> (literal-cons)
(1 . 5)
推荐答案
简短的回答是,是的,这是实现特定的行为.如数据的意外持久性,
The short answer is that yes, this is implementation specific behavior. As discussed in Unexpected persistence of data,
quote
:
如果文字对象(包括引号)的后果是不确定的 对象)进行破坏性的修改.
The consequences are undefined if literal objects (including quoted objects) are destructively modified.
这意味着您从此类功能中看到的任何行为都是特定于实现的(即使某些行为在实现中比其他行为更常见).
This means that any behavior you see from such a function is implementation specific (even if certain behaviors are more common among implementations than others).
这篇关于这是字面弊端的特定于实现的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!