Clojure 仅在使用命名空间时抛出错误

发布于 2024-11-02 18:53:57 字数 895 浏览 1 评论 0 原文

我已经开始在 Clojure 中开发国际象棋游戏,但我的命名空间遇到了问题。在我的文件顶部,我

(ns chess.core
    (:require clojure.contrib.str-utils2))

还定义了几个函数,包括 to-string,它将游戏块的符号转换为字符串。然而,当我编译时:

core.clj:21:8:
  error: java.lang.Exception: Unable to resolve symbol: to-string in this context (core.clj:21)

我做了一些实验,发现第一次调用我自己定义的函数时出现错误。我还发现,如果我注释掉 ns 调用,通常不会收到错误。有时可以通过重新启动 Swank 服务器来修复(有时不能)。有一段时间我只有 (ns chess.core),它抛出了同样的错误,所以我将其注释掉并继续黑客攻击。但现在我需要大写一些东西,所以我需要 str-utils。

说到 str-utils,我使用 Leiningen,并在 :dependencies 下的 project.clj 中包含以下内容:

                 [org.clojars.jhowarth/clojure-contrib "1.2.0-RC3"]]
;                [clojure.contrib.str-utils2 "1.2.1"]]

顶部的一个有效,底部的一个无效。

所以我觉得我只是对 Clojure 命名空间和库的工作方式一无所知,但与此同时其他人似乎都在成功使用 (ns foo.bar)

I've started working on a chess game in Clojure, but am having trouble with my namespace. At the top of my file, I have

(ns chess.core
    (:require clojure.contrib.str-utils2))

and also define several functions, including to-string, which turns the symbol for a game piece into a string. However, when I compile:

core.clj:21:8:
  error: java.lang.Exception: Unable to resolve symbol: to-string in this context (core.clj:21)

I've done some experimenting, and discovered that I get an error the first time I call a function that I defined myself. I've also discovered that I usually don't get an error if I comment out the ns call. Sometimes it can be fixed by restarting the Swank server (and sometimes it can't). For a while I just had (ns chess.core), which threw the same error, so I commented it out and continued hacking. But now I need to upper-case something, so I need str-utils.

Speaking about str-utils, I use Leiningen, and have the following in project.clj under :dependencies:

                 [org.clojars.jhowarth/clojure-contrib "1.2.0-RC3"]]
;                [clojure.contrib.str-utils2 "1.2.1"]]

The top one works, the bottom one doesn't.

So I feel like I'm just ignorant about how Clojure namespacing and libraries work, but at the same time everyone else seems to be using (ns foo.bar) successfully.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-11-09 18:53:57

编译时,定义从上到下进行评估。如果您引用文件中稍后定义的函数,您将收到该错误。我认为通过删除顶部的 ns,您实际上是在引用另一个命名空间(可能是默认用户 ns)中具有相同名称的函数。

如果您无法将函数移至首次使用的上方,则可以在顶部执行 (declare to-string) 并稍后定义它。

至于str-utils2包,它应该已经在clojure-contrib 1.2X中了。从 1.3(尚未完成)开始,contrib 将被拆分。请参阅 http://groups.google.com/group/clojure/msg/c5cdfec990efb6f4

When compiling, the definitions are evaluated from top to bottom. If you refer to a function that is defined later in the file, you'll get that error. I'm thinking that by removing the ns at the top, you're actually referring to a function with the same name in another namespace (probably the default user ns).

If you can't move the function to above its first use, you can do a (declare to-string) at the top and define it later.

As for the str-utils2 package, it should already be in clojure-contrib 1.2X. Starting from 1.3 (which isn't finished), contrib will be split up. See http://groups.google.com/group/clojure/msg/c5cdfec990efb6f4

深海夜未眠 2024-11-09 18:53:57

如果没有看到您对 to-string 函数的使用,就很难说,但是您可以查看一下关于 requirens使用工作:http://blog.8thlight.com/articles/2010/12/6/clojure-libs-and-namespaces-require-use-import-and-ns。它捕获了我个人发现的关于库和命名空间的令人困惑的事情。

It's hard to say without seeing your use of the to-string function, but you might check out this explanation of how require, ns, and use work: http://blog.8thlight.com/articles/2010/12/6/clojure-libs-and-namespaces-require-use-import-and-ns. It captures the things I personally found confusing about libs and namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文