简体   繁体   中英

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.

Right now I am using lein repl on a typical lein new app project context. I have created a testing.clj file next to my core.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:

(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? 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

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) is using the Java constructor, which requires importing the Java class separately. The Java class is created automatically when you define a record to allow Java interop with things you've defined in Clojure.

Using the (->Something 0) syntax is the correct way to go and should be possible after you (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
  2. In testing/src/testing you create testing.clj containing the following two lines

     (ns testing.testing) (defrecord Something [n]) 
  3. Run lein repl from within your project directory
  4. Use the namespace with (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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