【问题标题】:Why can I not access my Leiningen dependencies with Clojure code?为什么我无法使用 Clojure 代码访问我的 Leiningen 依赖项?
【发布时间】:2012-12-31 19:50:59
【问题描述】:

在 Clojure 中,如果我想引入 clojure.inspector 函数,我可以这样:

(use `[clojure.math.numeric-tower :include (expt)])

从 REPL,我现在可以评估函数 expt。

但是,在我看来,应该(并且可能是)另一种方法来做到这一点 - 使用 Leiningen 依赖项拉入代码。

我将此行添加到我的 project.clj:

[org.clojure/math.numeric-tower "0.0.2"]

然后我重新启动 REPL 以引入新的依赖项。为了安全起见,我什至做了“lein deps”(该命令没有输出)。当我尝试评估 expt 时,它给了我一个 RuntimeException,并说它无法解析符号。

如何才能访问 expt 函数,只使用 Leiningen 依赖项?

【问题讨论】:

    标签: math clojure read-eval-print-loop leiningen


    【解决方案1】:

    你不能。它不是那样工作的。添加依赖项会将代码放在您的 classpath 中,这仅意味着它可供您使用。为了真正使用命名空间内的东西,你需要使用

    (require '[the-namespace :refer [the things you want to use]])
    

    (require '[the-namespace :as tn])
    (tn/somevar)
    

    或者在 ns 声明中做这些事情中的任何一个(当不在 REPL 中并使用文件时)

    (ns foo
      (:require [the-namespace :as tn]))
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 2021-01-01
      • 2023-04-01
      • 2021-10-06
      • 2014-12-31
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多