1

When using gen-class this compiles fine:

(ns clj.sandbox)

(defn -hello
  [this]
  "Hello World")

(gen-class
  :name com.sandbox.GeneratedClass
  :methods [[hello [] String]])

But if you do this:

(ns clj.sandbox)

(def my-methods (atom [[hello [] String]]))

(defn -hello
  [this]
  "Hello World")

(gen-class
  :name com.sandbox.GeneratedClass
  :methods @my-methods)

You get this error:

CompilerException java.lang.RuntimeException: Unable to resolve symbol: hello in this context, compiling:(clj\sandbox.clj:3:17)

Is there any way to get around this error? I'd like to be able to pass in the :methods value instead of defining it inline.


In case it matters, this is the pom.xml I'm using to generate this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    
    <artifactId>sandbox</artifactId>
    <groupId>com.sandbox</groupId>
    <version>1.0-SNAPSHOT</version>    
    <packaging>clojure</packaging>
    <modelVersion>4.0.0</modelVersion>    
    <build>        
        <plugins>
            <plugin>
                <groupId>com.theoryinpractise</groupId>
                <artifactId>clojure-maven-plugin</artifactId>
                <version>1.3.13</version>
                <extensions>true</extensions>
                <configuration>
                  <sourceDirectories>
                    <sourceDirectory>src</sourceDirectory>
                  </sourceDirectories>
                </configuration>
            </plugin>
        </plugins>        
    </build>
    <dependencies>        
        <dependency>
            <groupId>org.clojure</groupId>
            <artifactId>clojure</artifactId>
            <version>1.5.1</version>
        </dependency>
    </dependencies>
</project>
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • I took off the "-" behind the hello function, so I had (defn hello [this] ...) and there was no error. – dizzystar Apr 24 '13 at 18:35
  • @dizzystar yes, that will run, but it won't be able to generate the class because it won't be able to find the implementation. – Daniel Kaplan Apr 24 '13 at 19:37
  • Perhaps look into multimethods. http://clojure.org/multimethods and the cheatsheet is always a life-saver: http://clojure.org/cheatsheet. Hope that helps, but I'm not entirely sure what you are trying to accomplish. – dizzystar Apr 25 '13 at 03:48
  • @dizzystar I don't see how multimethods are related to this – Daniel Kaplan Apr 25 '13 at 04:51
  • gen-class is a macro, and expects things like the method list to be available at compile time. You could perhaps compute your method list and expand it into a gen-class form via your own macro, but I'm not sure yet why you're wanting you do this at all. – Chouser Apr 25 '13 at 05:18
  • @Chouser: I was hoping to make a library that takes clojure code that looks (perhaps exactly) like compojure routes and generates it into Java classes that implement Jersey code. I wanted each `GET` you write to generate a method in a Jersey class. Here's a page to show you what Jersey source looks like (it's simple): https://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e47 @amalloy already told me I should do this in Java because it's too much trouble in clojure, but that was after I wrote this Q. I'd like to know more/if you agree. I already figured out the annotation part – Daniel Kaplan Apr 25 '13 at 06:59
  • 1
    Don't let cranky old men like @amalloy ruin your dreams. :-) If you succeed, the results may not be anything you'd actually want to use, but the journey getting there will doubtless be instructive. You will need to write a macro, doubtless a rather complex one. I'd recommend writing out the kind of complex gen-class form you want to produce, and the equivalent macro call that should produce it. Finally, write plain functions that convert from one to the other and a tiny macro to call those functions. Or find something more practical to do with your time. :-) Either way, good luck! – Chouser Apr 26 '13 at 04:27

1 Answers1

2

Because gen-class is a macro and passing @my-methods to it will cause the macro to get (deref my-method) as value of method argument which is not what is expected. You need to create a wrapper macro and then call it as shown below:

(defn -hello []
  "Hello world")

(def my-methods (atom '[[hello [] String]]))

(defmacro my-class []
  `(gen-class
    :name com.sandbox.GeneratedClass
    :methods ~(deref my-methods)))

(my-class)

Also note that the atom value is quoted otherwise you will get hello not found exception because it tries to resolve hello var which is not there.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • OK thanks a lot for the reply. I'll try that out very soon. Out of curiosity, how could I have debugged that myself? I tried running `macroexpand-1` but it just returned `nil`. – Daniel Kaplan Apr 25 '13 at 07:02
  • `gen-class` macro doesn't emit any code, it just execute some code that create the class file and return nil – Ankur Apr 25 '13 at 07:10