确定函数的调用者的命名空间 [英] Determine namespace of a function's caller

查看:113
本文介绍了确定函数的调用者的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试准确地确定函数的调用者的命名空间。看起来像 * ns * 由调用堆栈顶部的命名空间决定。

I'm trying to accurately determine the namespace of a function's caller. Looks like *ns* is determined by the namespace at top of the call stack.

user=> (ns util)
nil
util=> (defn where-am-i? [] (str *ns*))
#'util/where-am-i?
util=> (ns foo (:require [util]))
nil
foo=> (util/where-am-i?)
"foo"
foo=> (ns bar)
nil
bar=> (defn ask [] (util/where-am-i?))
#'bar/ask
bar=> (ask)
"bar"
bar=> (ns foo)
nil
foo=> (util/where-am-i?)
"foo"
foo=> (bar/ask)
"foo"
foo=>

是否还有其他元数据可以依赖或者需要手动指定? p>

Is there some other meta data I can rely on or do I need to specify this manually?

推荐答案

这是不可能的。在repl中, * ns * 始终设置为repl所在的命名空间;在运行时它通常是clojure.core,除非有人去麻烦设置它,这是不常见的。

This is not possible. In the repl, *ns* is always set to the namespace in which the repl is; at runtime it is usually clojure.core, unless someone goes to the trouble to set it, which is uncommon.

这篇关于确定函数的调用者的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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