Search code examples
clojure

gen-class in clojure and use it immediately


Is it possible to use gen-class to create a class without compiling it? I'm following a tutorial for orbit and am getting stuck here: https://github.com/orbit/orbit/wiki/Getting-Started%3A-Hello-World#actor-implementation.

I can use gen-interface to make an interface:

(import cloud.orbit.actors.Actor)
(import cloud.orbit.actors.runtime.AbstractActor)
(import cloud.orbit.concurrent.Task)

(gen-interface
 :name example.Hello
 :extends [cloud.orbit.actors.Actor]
 :methods [[sayHello [String] cloud.orbit.concurrent.Task]])

but gen-class doesn't work and I'm getting stuck

(gen-class
 :name example.HelloActor
 :extends [cloud.orbit.actors.runtime.AbstractActor]
 :implements [example.Hello]
 :methods [[sayHello [String] cloud.orbit.concurrent.Task]])

Solution

  • In the repl you could also do:

    (def original-compile-files *compile-files*)
    (alter-var-root #'*compile-files* (constantly true))
    (gen-class
     :name example.HelloActor
     :extends [cloud.orbit.actors.runtime.AbstractActor]
     :implements [example.Hello]
     :methods [[sayHello [String] cloud.orbit.concurrent.Task]])
    (alter-var-root #'*compile-files* (constantly original-compile-files))
    

    Then the example.HelloActor class should be available on the repl classpath immediately.

    This has implications of course in that you have to alter the var root, which is not ideal. In this example I just set it back to its default of false when done.