Common Lisp从包导出符号 [英] Common Lisp exporting symbols from packages

查看:166
本文介绍了Common Lisp从包导出符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法从包中导出所有符号,或者是在 defpackage 中执行的唯一方法。我通常将我的代码写入一个文件 foo.lisp ,通常以(in-package:foo)开头一个包定义到一个文件 package.lisp ,这通常涉及到这样一个例子:

 (in-package:cl-user)

(defpackage:foo
(:use:cl)
(:documentationBla bla bla。
(:export:* global-var-1 *
:* global-var-2 *
:function-1
:function-2
:struct
:struct-accessor-fun-1
:struct-accessor-fun-2
:struct-accessor-fun-3
:struct-accessor-fun-4))
我的问题是:设计一个使用一些全局变量和函数的界面有时可能不够,你必须导出一些结构,在这种情况下,如果不简单地导出这个结构体的访问器函数,则不能操纵这些结构体的对象,所以是有没有手动导出所有这些访问器函数的简单方法?

解决方案

一旦创建了包,并且所有符号在其中创建,例如,通过加载实现包的代码,您可以导出任何您喜欢的符号,例如导出全部:

 (do-all-symbols(sym(find-package:foo) )(出口sym))

您可能会更快乐

 (let((pack(find-package:foo)))
(do-all-symbols(sym pack) -package sym)pack)(export sym))))

不会尝试重新导出从二手包中的所有东西。


Is there a short way of exporting all the symbols from a package or is it the only way to do it in defpackage. I generally write my code in a file foo.lisp which generally starts with (in-package :foo) and put the package definition to a file package.lisp which generally involves something like this:

(in-package :cl-user)

(defpackage :foo
  (:use :cl)
  (:documentation "Bla bla bla."
  (:export :*global-var-1*
           :*global-var-2*
           :function-1
           :function-2
           :struct
           :struct-accessor-fun-1
           :struct-accessor-fun-2
           :struct-accessor-fun-3
           :struct-accessor-fun-4))

My question is: Designing simply an interface using some global variables and functions may not be adequate sometimes, and you have to export some structures. When this is the case, if you don't simply export accessor functions of this struct, you can not manipulate objects of these struct. So, is there an easy way for accomplishing this effect without manually exporting all these accessor functions?

解决方案

Once the package is created, and all symbols in it created, e.g., by loading your code that implements the package, you can export any symbols you like, e.g., to export all:

(do-all-symbols (sym (find-package :foo)) (export sym))

You'll probably be happier with

(let ((pack (find-package :foo)))
  (do-all-symbols (sym pack) (when (eql (symbol-package sym) pack) (export sym))))

which won't try re-exporting everything from used packages.

这篇关于Common Lisp从包导出符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆