在slime REPL中使用clojure.contrib函数 [英] Using clojure.contrib functions in slime REPL
问题描述
我想在REPL中使用slime中clojure.contrib.trace命名空间中的函数。如何让泥煤自动加载?一个相关的问题,我如何添加一个特定的命名空间到正在运行的repl?
在clojure.contrib API它描述的用法如下:
(ns my-namespace
(:require clojure.contrib.trace))
但是将此添加到我的代码会导致文件无法从跟踪命名空间加载任何函数的无法解析符号错误。
我使用leiningen'lein swank'启动ServerSocket,并且project.clj文件如下所示:
(defproject test-project0.1.0
:descriptionConnect 4用Clojure编写的代理
:dependencies [[org.clojure / clojure1.2.0-master-SNAPSHOT ]
[org.clojure / clojure-contrib1.2.0-SNAPSHOT]]
:dev-dependencies [[leiningen / lein-swank1.2.0-SNAPSHOT]
[swank-clojure1.2.0]])
一切似乎都是最新的, deps'不产生任何变化。那么是什么?
-
因为
:require
不会从给定的命名空间中提取任何Vars,它只会使命名空间本身可用。
因此,如果你
ns
表单中的(:require foo.bar)
,你必须写foo.bar/quux
可从命名空间foo.bar中访问Var
quux
>。您还可以使用(:require [foo.bar:as fb])
以将其缩短为fb / quux
。最终可能是写(:use foo.bar)
;这使得所有来自foo.bar
的变量在您的命名空间中可用。注意,它一般被认为是不好的风格,:use
外部库; -
Re:自动在REPL上提供资料:
:require
,:use
和:refer
ns
表单在require
,use
和指向
函数。还有对应于clojure.core
中的:refer-clojure
和:import
的宏。
这意味着为了使
clojure.contrib.trace
在REPL可用,你可以做类似(require'clojure。 contrib.trace)
或(require'[clojure.contrib.trace:as trace])
。注意,因为require
是一个函数,你需要引用库规范。 (使用
和引用
也可以引用lib规范;import
和refer-clojure
不需要引用。)
每次启动时都有某些命名空间可用的最简单方法Clojure REPL(包括当你使用SLIME时)在
〜/ .clojure / user.clj $ c中放置适当的
require
$ c>。有关详细信息,请参阅要求所有可能的命名空间博文Lawrence Aspden说明你可以在user.clj
中输入全部的贡献(我不这样做,虽然我有一个(使用'clojure.contrib.repl-utils)
在那里)。
I want to use the functions in the clojure.contrib.trace namespace in slime at the REPL. How can I get slime to load them automatically? A related question, how can I add a specific namespace into a running repl?
On the clojure.contrib API it describes usage like this:
(ns my-namespace
(:require clojure.contrib.trace))
But adding this to my code results in the file being unable to load with an "Unable to resolve symbol" error for any function from the trace namespace.
I use leiningen 'lein swank' to start the ServerSocket and the project.clj file looks like this
(defproject test-project "0.1.0"
:description "Connect 4 Agent written in Clojure"
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
:dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]
[swank-clojure "1.2.0"]])
Everything seems up to date, i.e. 'lein deps' doesn't produce any changes. So what's up?
You're getting "Unable to resolve symbol" exceptions because
:require
doesn't pull in any Vars from the given namespace, it only makes the namespace itself available.Thus if you
(:require foo.bar)
in yourns
form, you have to writefoo.bar/quux
to access the Varquux
from the namespacefoo.bar
. You can also use(:require [foo.bar :as fb])
to be able to shorten that tofb/quux
. A final possiblity is to write(:use foo.bar)
instead; that makes all the Vars fromfoo.bar
available in your namespace. Note that it is generally considered bad style to:use
external libraries; it's probably ok within a single project, though.Re: automatically making stuff available at the REPL:
The
:require
,:use
and:refer
clauses ofns
forms have counterparts in therequire
,use
andrefer
functions inclojure.core
. There are also macros corresponding to:refer-clojure
and:import
.That means that in order to make
clojure.contrib.trace
available at the REPL you can do something like(require 'clojure.contrib.trace)
or(require '[clojure.contrib.trace :as trace])
. Note that becauserequire
is a function, you need to quote the library spec. (use
andrefer
also take quoted lib specs;import
andrefer-clojure
require no quoting.)The simplest way to have certain namespaces available every time you launch a Clojure REPL (including when you do it with SLIME) is to put the appropriate
require
calls in~/.clojure/user.clj
. See the Requiring all possible namespaces blog post by John Lawrence Aspden for a description of what you might put inuser.clj
to pull in all of contrib (something I don't do, personally, though I do have a(use 'clojure.contrib.repl-utils)
in there).
这篇关于在slime REPL中使用clojure.contrib函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!