下面的代码“编译”,但不能正常运行:
(defstruct (image-info
(:conc-name img-)
(:constructor %make-img-info (&key file))
(:print-function print-img-info))
(file nil :type string)
(gd-image nil :type (or cl-gd::image null))
(size '(0 . 0) :type cons)
(position '(0 . 0) :type cons))
(defun print-img-info (info stream level)
(declare (ignore level))
(let ((size (img-size info))
(pos (img-position info)))
(format stream "~s[width: ~d, height: ~d, x: ~d, y: ~d]"
(img-file info)
(car size) (cdr size) (car pos) (cdr pos))))
(defun make-img-info (path)
(let ((image (cl-gd:create-image-from-file path))
(info (%make-img-info :file path))) ; <--- problem here
(setf (img-gd-image info) image
(img-size info)
(cons (cl-gd:image-width image)
(cl-gd:image-height image))) info))
SBCL 正确推断 的参数类型%make-img-info
,如下所示:
(describe '%make-img-info)
SPRITESHEET::%MAKE-IMG-INFO
[symbol]
%MAKE-IMG-INFO names a compiled function:
Lambda-list: (&KEY (FILE NIL))
Declared type: (FUNCTION (&KEY (:FILE STRING))
(VALUES IMAGE-INFO &OPTIONAL))
但是当我尝试编译时make-img-info
,我得到了这个:
note: deleting unreachable code
warning:
Derived type of PATH is
(VALUES CL-GD::IMAGE &OPTIONAL),
conflicting with its asserted type
STRING.
我将正确的参数(一个字符串)传递给这个函数,但它仍然无法调用它,因为它“相信”它必须是cl-gd:image
. 我怀疑问题在于布局以某种方式按字母顺序排列,并且gd-image
出现file
在列表之前......但是我该如何解决这个问题?我真的不想重命名该字段?