How to export slots and accessors from Lisp classes?

痴心易碎 提交于 2019-12-10 17:56:16

问题


This is my class's package:

(in-package :cl-user)
(defpackage foo
  (:use :cl)
  (:export :bar))
(in-package :foo)

(defclass bar ()
  (baz))

I can create an instance of bar in package cl-user.

CL-USER> (defvar f)
F
CL-USER> (setf f (make-instance 'foo:bar))
#<FOO:BAR {10044340C3}>

But I can't access the member baz. Calling slot-value like so ...

CL-USER> (slot-value f 'baz)

... results in this error message:

When attempting to read the slot's value (slot-value), the slot
BAZ is missing from the object #<FOO:BAR {10044340C3}>.
   [Condition of type SIMPLE-ERROR]

I already tried to add baz to the :export list but that does not work either.

How to export slots and accessors from packages?


回答1:


You can't export slots and accessors.

In Common Lisp you can export symbols.

So, export the symbol BAZ which names the slot.

Then in package CL-USER:

(slot-value some-instance 'foo:baz)

Unexported you have to write:

(slot-value some-instance 'foo::baz)

If you import the symbol into the package CL-USER:

(slot-value some-instance 'baz)


来源:https://stackoverflow.com/questions/11351443/how-to-export-slots-and-accessors-from-lisp-classes

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