简体   繁体   English

Clojure REPL和工作流程

[英]Clojure REPL and workflow

Coming from Haskell, my usual workflow would be to :l <file name.hs> on ghci and use the functions and ADT that I have there. 来自Haskell,我通常的工作流程是在ghci上:l <file name.hs>并使用我在那里的功能和ADT。

Right now I am using lein repl on a typical lein new app project context. 现在,我在典型的lein new app project上下文中使用lein repl I have created a testing.clj file next to my core.clj . 我在core.clj旁边创建了一个testing.clj文件。 There I defined a couple of functions, a protocol and a record implementing the protocol. 我在那里定义了两个功能,一个协议和一个实现该协议的记录。 I was able to use the function by (use 'testing.testing :reload) the problem is that I am not able to use the actual record: 我能够通过(use 'testing.testing :reload)使用该功能,但问题是我无法使用实际记录:

(def c (Something. 0))

I get: 我得到:

CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: Something

So, what would be the a "better" workflow in this case? 那么,在这种情况下,“更好”的工作流程是什么? Where I don't want to set the functions, protocols, records directly on the REPL, but also I don't want to rely on my core.cls file? 在我不想直接在REPL上设置功能,协议,记录的地方,但是我也不想依赖我的core.cls文件? I just want a file where I can dump a bunch of stuff and play with it. 我只想要一个文件,我可以在其中转储一堆东西并使用它。

PS: My env is Mac OSX Terminal + Sublime PS:我的环境是Mac OSX Terminal + Sublime

Edit: After a couple of minutes I was able to load the record by: 编辑:几分钟后,我可以通过以下方式加载记录:

  1. (load-file <file name>)
  2. (import 'testing.testing.Something)

I mean, for sure there is a better way than this... :/ I just want to load everything. 我的意思是,肯定有比这更好的方法了……:/我只想加载所有内容。 On the other hand I am able to use the protocol methods the record implements. 另一方面,我能够使用记录实现的协议方法。

Have you tried using the convenience function that is automatically defined for creating records? 您是否尝试过使用为创建记录自动定义的便捷功能? In this example it would be (->Something 0) . 在此示例中,它将是(->Something 0)

(Something. 0) is using the Java constructor, which requires importing the Java class separately. (Something. 0)正在使用Java构造函数,该构造函数要求分别导入Java类。 The Java class is created automatically when you define a record to allow Java interop with things you've defined in Clojure. 当您定义记录以允许Java与您在Clojure中定义的内容互操作时,会自动创建Java类。

Using the (->Something 0) syntax is the correct way to go and should be possible after you (use 'testing.testing :reload) . 使用(->Something 0)语法是正确的方法, (use 'testing.testing :reload)后应该可以(use 'testing.testing :reload)

Edit Given the above didn't seem to help, here's some step-by-step instructions to get a minimal working example 编辑鉴于以上内容似乎无济于事,请按以下逐步说明操作,以获取一个最小的工作示例

  1. You have an app directory testing created with lein new app testing 你有一个应用程序目录testing与创建lein new app testing
  2. In testing/src/testing you create testing.clj containing the following two lines testing/src/testing ,创建包含以下两行的testing.clj

     (ns testing.testing) (defrecord Something [n]) 
  3. Run lein repl from within your project directory 从项目目录中运行lein repl
  4. Use the namespace with (use 'testing.testing :reload) 将名称空间与(use 'testing.testing :reload)一起(use 'testing.testing :reload)
  5. (:n (->Something 42)) will create an instance of Something and retrieve the value of its n member - in this case 42. (:n (->Something 42))将创建Something的实例并检索其n成员的值-在这种情况下为42。

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

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