简体   繁体   English

leiningen REPL中的访问库函数

[英]access library functions in leiningen REPL

For the development of a library I started from a lein project, invoked like so: 为了开发库,我从一个lein项目开始,像这样调用:

lein new mylib

if I call lein install now, I can access my library in other projects. 如果现在调用lein install ,则可以在其他项目中访问我的库。 But trying to immidiately test the functions I wrote failed: 但是试图立即测试我编写的功能失败了:

lein repl
...
(dir mylib.core)
Exception No namespace: mylib.core found  clojure.core/the-ns (core.clj:4008)

Do I have to add something to the project.clj file maybe? 我是否必须在project.clj文件中添加一些内容?

In order to use a library you must cause the code to be loaded - that it be on the classpath is not sufficient. 为了使用库,您必须使代码被加载-仅放在类路径上是不够的。

You can do this easily in an ns declaration in a file of course, but in the repl it can be easier to use (require '[my-lib.whatever :as w]) after which one can call (w/foo) (w/bar) etc. as expected. 您当然可以在文件的ns声明中轻松地做到这一点,但是在repl中,它可以更容易使用(require '[my-lib.whatever :as w])之后可以调用(w/foo) (w/bar)等。 You can also use (in-ns 'my-lib.whatever) in order to switch to the namespace, but this will not give you a good result unless you have previously used require or use or load-file etc. to get the definitions first. 您也可以使用(in-ns 'my-lib.whatever)来切换到名称空间,但是除非您之前使用过requireuseload-file等来获取定义,否则这不会给您带来良好的结果。第一。

You need to add a dependency to use your library from another project. 您需要添加一个依赖项才能使用另一个项目中的库。 To do this add a vector (a tuple-2) to the vector that is the value of the :dependencies key in the project.clj file. 为此,将一个向量(元组2)添加到向量中,该向量是project.clj文件中:dependencies键的值。 Here's an example: 这是一个例子:

:dependencies [[org.clojure/clojure "1.7.0"]
               [org.clojure/clojurescript "1.7.170"]
               [org.clojure/core.async "0.2.371"]
               [default-db-format "0.1.0-SNAPSHOT"]
               [com.andrewmcveigh/cljs-time "0.3.14"]]

My own local library is called default-db-format . 我自己的本地库称为default-db-format Its really no different to adding a dependency for com.andrewmcveigh/cljs-time . 确实与为com.andrewmcveigh/cljs-time添加依赖项没有什么不同。

As you say you can already do this, but are having trouble getting a REPL connection to the project of the library itself. 正如您所说,您已经可以执行此操作,但是在与库本身的项目建立REPL连接时遇到了麻烦。 When you go (in-ns 'some-path) , you need the single quote in front of some-path . 当您走(in-ns 'some-path) ,您需要在some-path前面加上单引号。 Note that some-path is a different thing to the name of your library. 请注意, some-path与库名称不同。

Rather than use lein repl you can use the figwheel repl - if your project is setup with figwheel. 除了使用lein repl您还可以使用figwheel repl-如果您的项目是使用figwheel设置的。 My library has only one entry point and that is lein figwheel devcards . 我的图书馆只有一个入口,那就是lein figwheel devcards After that I had no problem going to a namespace and trying out a function: 在那之后,我可以毫无疑问地进入一个命名空间并尝试一个函数:

cljs.user=> (in-ns 'default-db-format.core)
nil
default-db-format.core=> (check 1 2)

As noisesmith mentioned having a REPL in your IDE is the best setup. 正如Noisesmith所说,在IDE中安装REPL是最好的设置。 No fiddly typing just bring up pre-configured REPLs (per namespace) with the click of a button (or keystroke). 只需轻按一下按钮(或击键)即可调出预先配置的REPL(每个名称空间),这不是很麻烦的输入。 Figwheel/Cursive setup instructions here . Figwheel / Cursive设置说明在此处

Let's say you created a new library named clj-foo . 假设您创建了一个名为clj-foo的新库。

% lein new clj-foo

Start your repl. 开始您的代表。

% cd clj-foo
% lein repl

In the repl, load the main entry point to your library and switch to its namespace. 在repl中,将主入口点加载到库中并切换到其名称空间。

(load-file "src/clj_foo/core.clj")
(ns clj-foo.core)

Now you're in the clj-foo.core namespace, make sure to add back in the repl ns to get things like doc available. 现在,您位于clj-foo.core命名空间中,请确保在repl ns中重新添加以使诸如doc可用。

(use 'clojure.repl)

That's it. 而已。 You're all set to start calling functions in your library. 您已经准备好开始在库中调用函数。 Note that other library files will be available from the clj-foo.core namespace if they were loaded by namespace declaration at the top of clj_foo/core.clj . 请注意,如果其他库文件是通过clj_foo/core.clj顶部的命名空间声明加载的,则可以从clj-foo.core命名空间获得它们。 If not, then you'll need to invoke load-file with their path as well. 如果不是,那么您还需要调用load-file及其路径。

If you make changes in core.clj . 如果您在core.clj进行更改。 You can invoke load-file again to pick up the new code. 您可以再次调用load-file以获取新代码。 As you progress, you can use cider to facilitate loading of individual functions and files. 随着您的前进,您可以使用苹果酒来促进单个功能和文件的加载。 But that's for another question. 但这是另一个问题。 :) :)

I was also facing the same issue with the following configuration: 我还面临以下配置的相同问题:

Leiningen 2.9.0 on Java 1.8.0_201 Java HotSpot(TM) 64-Bit Server VM

My file looks like this, and from the repl I desired to invoke the foo function 我的文件如下所示,从REPL中我想调用foo函数

 (ns cljtest.test
  (:gen-class))

(defn foo [input]
  (assoc {} "a" 123)) 

Both these approaches worked fine for me on the repl. 这两种方法对我来说都很好。

1)Switch to the appropriate name space: 1)切换到适当的名称空间:

cljtest.core=> (in-ns 'cljtest.test)
#object[clojure.lang.Namespace 0x90175dd "cljtest.test"]
cljtest.test=> (foo nil)
{"a" 123}
cljtest.test=>

2)Require the appropriate name space: 2)要求适当的名称空间:

cljtest.core=> (require '[cljtest.test :as test])
nil
cljtest.core=> (test/foo nil)
{"a" 123}
cljtest.core=>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM