Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 如何将方法列表传递给gen类?_Clojure_Gen Class - Fatal编程技术网

Clojure 如何将方法列表传递给gen类?

Clojure 如何将方法列表传递给gen类?,clojure,gen-class,Clojure,Gen Class,当使用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 [t

当使用
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:无法解析符号:hello在此上下文中,正在编译:(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>

沙箱
com.sandbox
1.0-快照
clojure
4.0.0    
理论与实践
clojure maven插件
1.3.13
真的
src
org.clojure
clojure
1.5.1

因为gen类是一个宏,将@my methods传递给它会导致宏将
(deref my method)
作为方法参数的值,这不是预期的值。您需要创建一个包装宏,然后按如下所示调用它:

(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)

另外请注意,原子值被引用,否则您将得到
hello not found
异常,因为它试图解析不存在的hello var。

我去掉了hello函数后面的“-”,所以我有(defn hello[this]…),没有错误。@dizzystar是的,这将运行,但是它将无法生成类,因为它将无法找到实现。而且备忘单永远是救命稻草。希望这会有帮助,但我不完全确定你想要实现什么。@dizzystar我不知道多种方法与此有什么关系。不要让像@amalloy这样的脾气暴躁的老人毁掉你的梦想。:-)如果你成功了,结果可能不是你真正想用的东西,但到达那里的旅程无疑会有启发性。您需要编写一个宏,无疑是一个相当复杂的宏。我建议您写出您想要生成的复杂gen类表单,以及相应的宏调用。最后,编写从一个转换到另一个的普通函数,并编写一个小宏来调用这些函数。或者找些更实际的事情来打发时间。:-)不管怎样,祝你好运!好的,非常感谢你的回复。我很快就会试试的。出于好奇,我怎么能自己调试呢?我尝试运行
macroexpand-1
,但它只返回
nil
genclass
宏不发出任何代码,它只执行一些创建类文件的代码并返回nil