语言Forth提供了一个“编译时”转义机制,可以在编译器运行时(不在运行时)立即执行代码。例如,您可以包含打印语句,以调试棘手的语法或类型错误)。Clojure编译时转义机制
Clojure有类似的东西吗?我在我的一个函数调用中遇到了编译时IllegalArgumentException,并且希望添加一个编译时打印语句来确定参数类型((.getClass)
)。
谢谢。
UPDATE:这里是没有编制完整的defn
:
(ns my.ns.name
(:gen-class
:main true)
(:use
[clojure.contrib.str-utils2 :only (join)])
(:import
[java.io PrintWriter]
[java.net URL]
[java.util.concurrent Executors]
[java.util.jar Manifest]
[org.apache.commons.cli CommandLine HelpFormatter Options Option ParseException PosixParser]))
(defn set-version
"Set the version variable to the build number."
[]
(def version
(-> (str "jar:" (.. my.ns.name (getProtectionDomain)
(getCodeSource)
(getLocation))
"!/META-INF/MANIFEST.MF")
(URL.)
(.openStream)
(Manifest.)
(.. getMainAttributes)
(.getValue "Build-number"))))
这defn
作品:
(defn set-version
"Set the version variable to the build number."
[]
(println (str (.getClass my.ns.name)))
(def version
(-> (str "jar:" (-> my.ns.name (.getProtectionDomain)
(.getCodeSource)
(.getLocation))
"!/META-INF/MANIFEST.MF")
(URL.)
(.openStream)
(Manifest.)
(.. getMainAttributes)
(.getValue "Build-number"))))
mmm在编译时?你什么时候编译源代码来生成jvm字节码? clojure没有其他编译时间。我认为这个异常只能在运行时导致public class IllegalArgumentException extends RuntimeException。要在运行时检查类,请在参数声明向量后插入(println(type arg)) – jneira 2010-10-22 11:53:59
@jneira:我正在使用Maven构建项目。在AOT编译期间,肯定会发生**。 – Ralph 2010-10-22 12:08:53
好吧,我想念那是在其运行时抛出异常的java编译器 – jneira 2010-10-22 12:31:23