Created
December 11, 2012 03:02
-
-
Save kohyama/4255602 to your computer and use it in GitHub Desktop.
Check how things are going about expansion and loading in clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns check-inline | |
| (:gen-class)) | |
| (defmacro sqr-m [n] `(* ~n ~n)) | |
| (definline sqr-i [n] `(* ~n ~n)) | |
| (defn sqr-fwi | |
| {:inline (fn [n] `(do (println "inline:") (* ~n ~n)))} | |
| [n] (do (println "function:") (* n n))) | |
| (defn -main [& args] | |
| (let [n 3] | |
| (dorun | |
| (map println (list (sqr-m n) (sqr-i n) (sqr-fwi n)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns macro-without-class | |
| (:gen-class)) | |
| (defmacro eval-when-compile [& body] | |
| (binding [*compile-files* false] | |
| (eval `(do ~@body))) | |
| nil) | |
| (eval-when-compile | |
| (defmacro sqr-nc [n] `(* ~n ~n))) | |
| (defmacro sqr-c [n] `(* ~n ~n)) | |
| (defn -main [& args] | |
| (println (sqr-nc 3)) | |
| (println (sqr-c 3))) |
Author
Author
Mr. @athos0220 gave me a great help.
https://twitter.com/athos0220/status/278439357126418432
http://ideone.com/8mBm5N
% tree
.
├── classes
└── src
└── macro_without_class.clj
2 directories, 1 file
% cat src/macro_without_class.clj
(ns macro-without-class
(:gen-class))
(defmacro eval-when-compile [& body]
(binding [*compile-files* false]
(eval `(do ~@body)))
nil)
(eval-when-compile
(defmacro sqr-nc [n] `(* ~n ~n)))
(defmacro sqr-c [n] `(* ~n ~n))
(defn -main [& args]
(println (sqr-nc 3))
(println (sqr-c 3)))
% java -cp /path/to/clojure-1.4.0.jar:src:classes clojure.main
Clojure 1.4.0
user=> (compile 'macro-without-class)
macro-without-class
user=> ^D
% ls classes/
macro_without_class$_main.class
macro_without_class$eval_when_compile$fn__4.class
macro_without_class$eval_when_compile.class
macro_without_class$loading__4784__auto__.class
macro_without_class$sqr_c.class
macro_without_class.class
macro_without_class__init.class
% java -cp /path/to/clojure-1.4.0.jar:classes macro_without_class
9
9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hum...
(set! *compile-files* false)surpresses creation of classes for the macrosqr-m, but I can't use it.As for
definlineand{:inline}, results are similar to the case withdefmacro.