为什么REPL将clojure.core / doc视为var? [英] Why does REPL treat clojure.core/doc as a var?

查看:147
本文介绍了为什么REPL将clojure.core / doc视为var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Clojure doc 函数获取文档,但无法从REPL中识别(我使用Emacs和SLIME)。以下序列描述了发生的情况(错误消息紧跟在每行之后):

I'm trying to get documentation using the Clojure doc function, but can't get it recognized from the REPL (I'm using Emacs and SLIME). The following sequence describes what's going on (error message follows immediately after each line):

gaidica.core> (doc first)
; Evaluation aborted.

Unable to resolve symbol: doc in this context
  [Thrown class java.lang.Exception]

gaidica.core> (clojure.core/doc first)
; Evaluation aborted.

No such var: clojure.core/doc
  [Thrown class java.lang.Exception]

user> (clojure.core/doc first)
; Evaluation aborted.

No such var: clojure.core/doc
  [Thrown class java.lang.Exception]

user> (doc first)
-------------------------
clojure.core/first
([coll])
  Returns the first item in the collection. Calls seq on its
    argument. If coll is nil, returns nil.
nil
user> 

如何引用 doc 函数并将其识别为函数而不是变量?

How do I refer to the doc function and get it recognized as a function, not a variable?

ADDENDUM,6/22/11,问题后9小时发布

ADDENDUM, 6/22/11, 9 hours after question posted

@kotarak做出了最相关的评论:注意,clojure.core / doc是1.2及更早版本,clojure.repl / doc是1.3及更高版本。果然,以下工作:

@kotarak made the most relevant comment: "Note, that clojure.core/doc is 1.2 and earlier. clojure.repl/doc is 1.3 and later." Sure enough, the following worked:

user> (clojure.repl/doc first)
-------------------------
clojure.core/first
([coll])
  Returns the first item in the collection. Calls seq on its
    argument. If coll is nil, returns nil.
nil
user>

我能够确认Clojure 1.3是活动的:

I was able to confirm that Clojure 1.3 was active:

user> *clojure-version*
{:interim true, :major 1, :minor 3, :incremental 0, :qualifier "master"}
user> 

但是这也太混乱 - 我的Leiningen project.clj指定了Clojure 1.2!

But this too was confusing--my Leiningen project.clj had specified Clojure 1.2!

根据我自己的经验,我曾经注意到,即使我改变了相关目录的内容,基于SLIME的REPL挂在一个Java类路径值上。然后解决方案是退出Emacs和 lein swank ,然后重新输入两者,然后重试。我尝试一样,得到以下结果:

In my own experience, I once noticed that the SLIME-based REPL "hung onto" a Java classpath value even after I changed the contents of relevant directories. The solution then was to exit Emacs and lein swank, then re-enter both and try again. I tried the same and got the following result:

user> *clojure-version*
{:major 1, :minor 2, :incremental 0, :qualifier ""}
user> 

我唯一可以做的结论是,我之前的REPL使用了Clojure 1.3。我之前的工作的项目使用了一个Clojure 1.3快照,所以我猜,REPL已经挂到Clojure 1.3某种方式。

The only conclusion I can make is that my previous REPL had been using Clojure 1.3. The project that I worked on previous to this had used a Clojure 1.3 snapshot, so I'm guessing that the REPL had "hung onto" Clojure 1.3 somehow.

问题解决,经验教训等。对于奖励积分,任何人都可以解释发生了什么的原因(使用Clojure 1.2和1.3)?

Problem solved, lesson learned, etc. For bonus points, can anybody explain the reason for what happened (with Clojure 1.2 vs. 1.3)?

对所有贡献者。

推荐答案

几个更正。首先,doc是一个宏不是一个函数。另外,函数和宏可以存储在var。第二,在clojure 1.3中,这很可能是你使用的版本,doc存储在var clojure.repl / doc,而不是clojure.core / doc(因为它在1.2)。在命名空间用户中,doc是used,也有一个隐式的(使用[clojure.repl:only [doc])。当你转到一个新的命名空间,或者甚至用ns创建一个命名空间时,clojure.repl / doc不会被自动添加,与使用'user'不同。

Couple of corrections. First, doc is a macro not a function. Also functions and macros can be stored in a var. Second, in clojure 1.3, which is likely the version you are using, doc is stored in the var clojure.repl/doc, not clojure.core/doc (as it is in 1.2). In the namespace user, doc is "used", aka there is an implicit "(use [clojure.repl :only [doc])". When you goto a new namespace, or even create one with ns, clojure.repl/doc is not automatically added, unlike it is with 'user.

关于以下问题:

为什么REPL将clojure.core / doc视为var?

clojure中的函数,宏和值或者存储在var中,或者绑定到一个符号,比如在let或者函数中。 clojure.core / doc是一个var,它保存了做什么文档的宏。

Functions, macros and values in clojure are either stored in vars or bound to a symbol such as in a let or function. clojure.core/doc is the var that holds the macro that does what doc does.

如何引用doc函数,函数,而不是变量?

像所有的lisps,做一个调用,无论是函数还是宏,列表的第一个位置。

Like in all lisps, to do a call, whether a function or a macro, you must put the function/macro in the first position of a list.

(<fn/macro> *args)

所以要调用宏文档本身,你会这样做:

So to call the macro doc on itself, you would do:

(doc doc)

这篇关于为什么REPL将clojure.core / doc视为var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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