common lisp: slot-value for defstruct structures

孤街浪徒 提交于 2019-12-01 16:21:50

Usually you would use accessor functions with structures.

Your code defines the accessor functions point-x and point-y. You can use those.

You can also use SLOT-VALUE with structures on implementations which support it. I guess that's most implementations (GCL would be an exception). There is Lisp software which assumes that SLOT-VALUE works for structures. I don't think implementations will remove support for it. It'a not in the standard, because some implementors wouldn't want to provide this functionality in deployed applications.

So both ways are okay.

If you want to have short names, go with accessors:

CL-USER 109 > (defstruct (point :conc-name)
                (x 0) (y 0))
POINT

CL-USER 110 > (make-point :x 5 :y 3)
#S(POINT :X 5 :Y 3)

CL-USER 111 > (setf p1 *)
#S(POINT :X 5 :Y 3)

CL-USER 112 > (x p1)
5

CL-USER 113 > (setf p2 (make-point :x 2 :y 3))
#S(POINT :X 2 :Y 3)

CL-USER 114 > (list p1 p2)
(#S(POINT :X 5 :Y 3) #S(POINT :X 2 :Y 3))

CL-USER 115 > (mapcar 'x (list p1 p2))
(5 2)

Name clashes between various accessor functions then would have to be prevented by a package.

If you want to write a shorter version of SLOT-VALUE, that's also fine. Write a macro. Or write an inlined function. Sure - why not?

As I said, SLOT-VALUE works with structures in most implementations. In this case you should not care that the ANSI CL spec does not define that. In many ways implementations extend the ANSI CL spec. For example by SLOT-VALUE working on structures, implementing streams as CLOS classes, implementing conditions as CLOS classes, providing a Meta-object Protocol, ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!