Hello,
And one more added benefit is that if you (or something using your
namespace) uses IDEs that auto-load (or auto-compile) the clj files each
time they are saved (such as clojuredev does), it would be impractical to
have a namespace auto-execute itself. Because then, the auto-load
functionality is bloated (counter performant, potentially blocking
everything, etc.)
Note that if you want a solution that can auto-execute when run standalone
but not auto-execute when just loaded, you can generate from your main
namespace a class with a static main method :
add a :gen-class directive in the ns declaration :
(ns temp-converter
(:gen-class)
(:import (java.awt BorderLayout Event GridLayout Toolkit)
(java.awt.event KeyEvent)
(javax.swing AbstractAction Action BorderFactory
JFrame JPanel JButton JMenu JMenuBar JTextField JLabel
KeyStroke)
(javax.swing.event DocumentListener)))
create a -main method that will be automatically recognised as the static
public void main(String[] args) classic java application start method :
(defn -main (main))
suppress the explicit call to (main), or guard it by tests to see if the ns
is loaded via compilation:
(when-not *compiling* (main))...
HTH,
--
Laurent
2009/3/15 Timothy Pratley <[email protected]>
>
> Hi Keith,
>
> IMO it is slightly better to use a function as you described. The
> benefit being that it makes it easier to test your helper functions
> without running the main application. For instance if you comment out
> (main) and load the file to the REPL or call a test function instead.
> It seems the defacto standard on clj files I've seen is to provide an
> application function commented out at the bottom of the file.
>
> What might be nice is to have a check for 'included' or 'loaded' vs
> 'executed'. This can almost be done by looking at *command-line-args*,
> however not quite because the standard launcher does not include $0.
> So you have to use a non-standard launcher which includes $0 to
> achieve this:
> (when *command-line-args*
> (main *command-line-args*))
>
> There is discussion about this previously:
>
> http://groups.google.com/group/clojure/browse_thread/thread/9ec09e7826992ea8/9e0085eb74061274
>
> http://groups.google.com/group/clojure/browse_thread/thread/ca60d98fb4f7e71e/16b0ebb277daf5b9
>
>
> Regards,
> Tim.
>
>
> On Mar 15, 12:55 pm, Keith Bennett <[email protected]> wrote:
> > Is it a good idea or a bad idea to provide a main() function as the
> > program's entry point?
> >
> > As an example, I have a program athttp://is.gd/ndTV. If you look at
> > the bottom you'll see (unless and until I change it) the specification
> > of a main function, and then a call to it. I'm aware that I could
> > just list the contents of main() outside any function, and it would
> > work the same way.
> >
> > So which approach is better, and why?
> >
> > Thanks,
> > Keith
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---