1

当使用gen-class这个编译罚款:

(ns clj.sandbox)

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

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

但如果你这样做:

(ns clj.sandbox)

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

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

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

你得到这个错误:

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

有没有办法解决这个错误?我希望能够传入:methods值而不是内联定义它。


如果它很重要,这是我用来生成它的 pom.xml:

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

1 回答 1

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.

于 2013-04-25T05:31:40.567 回答