【问题标题】:How to access a context of RESTAS submodules from other RESTAS modules如何从其他 RESTAS 模块访问 RESTAS 子模块的上下文
【发布时间】:2013-03-18 18:41:18
【问题描述】:

假设我在模块m1 中定义了一个动态变量*a*。然后我将模块 m1 安装在 m2 模块中:

(in-package m2)

(mount-module sub (#:m1)
   (m1:*a* 3))

我现在如何从模块 m2 中获取值 3?例如,目前,我必须以这样的方式破解它:

(restas:with-context
    (second (gethash 'm1
                     (gethash
                       :modules
                       (gethash (find-package :m2)
                                restas::*pkgmodules-traits*))))
    m1:*a*))

当然,这甚至不是一个好方法。

【问题讨论】:

    标签: web lisp common-lisp hunchentoot


    【解决方案1】:

    一个稍微好一点的解决方案如下:

    (defmethod restas:module-context ((module symbol))
          (second (gethash module (restas::pkgmodule-traits-modules *package*))))
    

    然后你可以用module-context访问模块的上下文,例如:

    (with-context (module-context 'sub)
      m1:*a*)
    

    或者更好:

    (with-module 'sub
      m1:*a*)
    

    请注意,您使用的是安装模块时使用的名称,而不是您安装的包的名称。

    更新:我描述的方法已添加到 RESTAS。

    【讨论】:

    • 确实更好,但还是要依赖内部函数restas::pkgmodule-traits-modules。我猜这个框架不是为这样的操作而设计的
    • module-context 方法现在在 RESTAS 中,所以你不再需要直接使用 pkgmodule-traits-modules。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多