从REPL(Cloure 1.4.0)我试图使用source
宏来显示我的函数的定义 - 但它回复'源未找到'
我可以用source
在source
自己喜欢这个(可以看到它使用了source-fn
) -但不知道为什么它不喜欢我的defn x[] "hello"
函数的定义?
user=> (source source)
(defmacro source
"Prints the source code for the given symbol, if it can find it.
This requires that the symbol resolve to a Var defined in a
namespace for which the .clj is in the classpath.
Example: (source filter)"
[n]
`(println (or (source-fn '~n) (str "Source not found"))))
nil
user=> (defn x[] "hello")
#'user/x
user=> (source x)
Source not found
nil
user=>
Run Code Online (Sandbox Code Playgroud)
source
只能获取类路径中可用的源文件中定义的函数源.它不适用于REPL定义的函数.
更准确地说,source
通过查找由其参数命名的Var,检查Var上的元数据映射是否包含源信息(一切都工作:file
和:line
需要密钥),查找元数据映射中指定的文件,打开文件. (作为类路径上的资源),跳过多行,最后返回下一个表单后面的文本; 看(source clojure.repl/source-fn)
详情.
因此,它适用于存储在Vars中的东西 - 比如函数和宏 - 在类路径中仍然存在的源文件的顶层定义.它不适用于未存储在Vars中的内容,也不适用于存储在Vars中的内容,这些Vars的类路径中不存在后备源.后一种情况可以通过AOT编译和在REPL中定义的内容来实现.
归档时间: |
|
查看次数: |
291 次 |
最近记录: |