defstruct common lisp 中的范围问题

问题描述 投票:0回答:1

我正在关注《光线追踪器挑战》这本书..我创建了一个 Vector3 和颜色 defstruct

(defstruct (vector3 
             (:conc-name nil)
             (:constructor v3 (x y z w)))
  (x 0.0 :type float)                                                                                                                                                              
  (y 0.0 :type float)                                                                                                                                                              
  (z 0.0 :type float)                                                                                                                                                              
  (w 0.0 :type float))                                                                                                                                                             
                                                                                                                                                                       
(defstruct (colors
             (:conc-name nil)
             (:constructor colors (r g b w)))                                                                                                    
  (r 0.0 :type float)                                                                                                                                                              
  (g 0.0 :type float)                                                                                                                                                              
  (b 0.0 :type float)                                                                                                                                                              
  (w 0.0 :type float))

以及其他一些功能,例如比例乘数

(defun vector-scalar (s v1)                                                                                                                                            
  (vector3 (* s (x v1))                                                                                                                                                
           (* s (y v1))                                                                                                                                                
           (* s (z v1))                                                                                                                                                
           (* s (w v1))))     

但是当我尝试加载文件时,我收到以下错误:

caught WARNING:                                                                                                                                                      
;   Derived type of COMMON-LISP-USER::V1 is                                                                                                                            
;     (VALUES COMMON-LISP-USER::VECTOR3 &OPTIONAL),                                                                                                                    
;   conflicting with its asserted type                                                                                                                                 
;     COMMON-LISP-USER::COLORS.                                                                                                                                        
;   See also:                                                                                                                                                          
;     The SBCL Manual, Node "Handling of Types"  

我认为过程中的任何内容都具有本地作用域,所以我不明白为什么我认为函数的输入会与

defstruct
混淆。如果我只加载向量
defstruct
我不会收到错误。

我担心的是,我创建的任何具有四个参数的

defstruct
都会与向量3

混淆

我不确定如何解决这个问题,因为我实际上不明白问题出在哪里。

common-lisp
1个回答
0
投票

您的两个结构定义都定义了一个名为

w
的访问器,因此该访问器不能存在。 如果您希望多个不同的类具有相同的访问器,则需要使用
defclass
而不是
defstruct
defclass
将访问器定义为泛型函数。

© www.soinside.com 2019 - 2024. All rights reserved.