gpt4 book ai didi

arrays - 在 SBCL 中读取带有类型槽的结构

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:48 25 4
gpt4 key购买 nike

考虑这个简单的代码示例:

(defstruct test
(:example nil :type (simple-array single-float)))

(defparameter test-struct
(make-test :example (make-array 10 :element-type 'single-float
:initial-element 1.0)))

如我们所见,这里没有什么疯狂的地方,定义了一个带有单个槽的简单结构。我们还指定 :example 插槽是强类型,预计是单个 float 数组。没有问题,一切正常。

现在让我们将test-struct 写入文件:

(with-open-file (out "~/Desktop/out.test"
:direction :output
:if-exists :supersede)
(format out "~s" test-struct))

同样,不出所料,我们在桌面上得到了一个漂亮的小文件,其中包含以下内容:

#S(TEST :EXAMPLE #(1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0))

然而,紧张情绪开始蔓延,因为我们注意到没有任何迹象表明这个特定的文字数组应该包含单个 float 。带着这种怀疑,让我们尝试加载这个结构:

(defparameter loaded-struct
(with-open-file (in "~/Desktop/out.test")
(read in nil)))

我们到了,SBCL 愉快地将我们放入调试器中:

SBCL drops into a debugger when attempting to read a typed array

问题是:有没有办法指示 SBCL 验证结构中的这个数组槽是有效类型并加载它?或者换句话说,是否可以在不借助构建自定义编写器和构造函数的情况下读取具有强类型插槽的结构?

最佳答案

如果您可读打印对象,它会起作用:

(equalp test-struct
(read-from-string
(with-output-to-string (o)
(write test-struct :readably t :stream o))))
=> T ;; no error while reading the structure back

字符串是:

"#S(TEST
:EXAMPLE #.(COERCE #(1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0)
'(SIMPLE-ARRAY SINGLE-FLOAT (*))))"

如果要使用格式,请使用~W,而*print-readably*为T。

关于arrays - 在 SBCL 中读取带有类型槽的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com