Clojure Development: IDE or REPL?

后端 未结 7 1228
野趣味
野趣味 2021-02-02 13:05

I\'ve been using Clojure for a little while and want to create some projects that are bigger and more complicated than toys. I\'ve been using Java for the past few years and hav

相关标签:
7条回答
  • 2021-02-02 13:47

    Well, to start off, virtually any decent development plugin for any editor or IDE will give you a way to use the Clojure REPL from within the IDE. Probably even allow you to load files into the REPL for testing and such. You don't need to choose one or the other.

    Enclojure is going a long way, that is for sure. However, most people are perfectly happy with using Emacs, including myself. Emacs uses a Lisp as its configuration language, so it's usually the most natural choice to a Lisper.

    For languages that have REPLs, using the REPL as a major part of the development process is the norm. Editing files, loading them into the REPL, playing with them to see if a work, rinse, repeat. This is one of the major advantages of languages with REPLs like Clojure and Haskell and CL and such.

    As for building jars, and compiling Clojure code and stuff, that's simple. You don't even really /have/ to Compile Clojure code most of the time if you don't want to. When you do, you AOT compile it with gen-class, which compiles it to class files that you can then put into a jar. There are tons of examples and even tutorials spread amongst the interwebs. The easiest and most efficient way is to use something like Ant and writing a build script that compiles Clojure code and generates the .jar for you. The first time I did it, I thought it was going to be hard to do, but it was actually really simple. I just looked at the Clojure and Clojure-Contrib Ant build files, and referenced the Ant help pages for everything else I needed.

    One thing I should mention is the fact that Enclojure /does/ actually build executable .jar files for you, if you request it to do so. I'm sure more advanced things you're used to will be added in the future. They are, indeed, still quite new.

    0 讨论(0)
  • 2021-02-02 13:55

    The REPL development approach has been encouraged by most LISP family (and other functional language) IDE's for a long time. Some of these REPL's also feature the auto-complete features you might associate with a Java IDE.

    REPL gives you a couple major advantages over the standard approach. The first is that it allows you to execute arbitrary code during the run-time of your program, which can make debugging multi-threaded systems much simpler. Second, and more importantly, it makes it easy to test your functions while you code. You don't need to build a framework around a new function or class, you can play with it directly in the REPL, and see how it responds to a variety of use cases.

    0 讨论(0)
  • 2021-02-02 13:56

    "When I look at examples of development in Clojure they seem to be along the lines of load files into the REPL, see how they work, edit the file, reload, repeat. Nary a class or jar being generated anywhere......I'd like to hear some work flow examples from folks who have created some non-trivial programs to be used by regular users (not other devs) if possible."

    The examples at Debugging clojure functions which are invoked in java code (eclipse) are trivial, but illustrate how in eclipse one can link projects such that one can invoke clojure/scala functionality from within java.

    0 讨论(0)
  • 2021-02-02 14:00

    I use both - Eclipse IDE and Counterclockwise plugin which provides a REPL. This is a particularly nice combination if you develop Java code alongside Clojure (as I do).

    My general approach is:

    • Write Clojure code in the editor
    • Keep a REPL open for testing things out (Ctrl+Enter is a useful shortcut here: it executes whatever code is selected in your editor in the REPL)
    • Use the generic IDE tools for project management, building, testing, SCM etc.

    At times , I also work purely in the REPL. This is usually better for testing things out quickly. If I particularly like a piece of test code, I will simply copy/paste it from the REPL into my test suite.

    0 讨论(0)
  • 2021-02-02 14:00

    With Eclipse, you can have unit tests (or any program) run every time you save a file in the project. The whole process takes less than a second -- it's great for test-driven development, and removes the need for cut-and-paste into the REPL for most situations. Plus you get to keep the tests! :-)

    You'll want to use cake to avoid the JVM startup time every time you launch your tests. You can run cake test on the command line, or just have Eclipse run it for you when you save any file in your project.

    I got this to work by setting up a project with Leiningen, installing cake, and installing the counterclockwise Eclipse plugin. To set up Eclipse to run tests every time you save, select Project->Properties->Builders, click New, select Program, call it something like 'run tests', add the path to cake under Location, select the Working Directory for your project, and add test to the optional arguments. Under the 'Build Options' tab, select 'During Auto Builds.'

    I got the general idea for this from Jeff Younker's book, "Foundations of Agile Python Development." Been an Emacs man for 20 years, but this trick has put Eclipse at the front of my workflow lately.


    Update: roughly one year later, I now use Midje with the lazytest option to do the same thing while developing with both Emacs and Eclipse.

    0 讨论(0)
  • 2021-02-02 14:07

    What I do is to create a project with Leiningen. If you just want to whack away for a while, you can just type lein repl and your project classpath will be set up for that repl.

    Actually though, I use Emacs, slime and swank clojure. After navigating to the project, you simply type M-x clojure-jack-in. This starts the swank server and slime connects to it and you have a repl in a buffer. Of course just that is not worth more than running a repl in a shell. What you do get is the ability to write code in another buffer, and with a keybinding select sexps or larger units and execute them in the repl to see if it works. Also slime gives you handy keybindings for common tasks. See swank-clojure on github for more info.

    JohnJ, You can do the same thing in Emacs. Actually that phrase is probably a tautology.

    (add-hook 'after-save-hook etc.
    

    Here is someone's example after-save-hook for git: https://gist.github.com/449668

    0 讨论(0)
提交回复
热议问题