Last active
October 20, 2015 17:45
-
-
Save patrickgombert/2e83b0acf390e0d7485b to your computer and use it in GitHub Desktop.
deftype with class names as vars and macroexpansion
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
(defn- symbol->Class [resolved-symbol original-symbol] | |
(if (class? resolved-symbol) | |
(-> (str resolved-symbol) | |
(split #"\.") | |
last | |
symbol) | |
original-symbol)) | |
(defn- do-resolve [sym] | |
(let [r (resolve sym)] | |
(if (class? r) | |
r | |
@r))) | |
(defmacro deftype [t bs & body] | |
(let [b (map #(cond | |
(symbol? %) | |
(symbol->Class (do-resolve %) %) | |
(list? %) | |
(macroexpand-1 %) | |
:else | |
%) | |
body)] | |
(list* 'clojure.core/deftype t bs b))) | |
(comment | |
(def object Object) | |
(defmacro is-equal? [return] | |
`(equals [this other] ~return)) | |
(deftype Foo [] | |
object | |
(is-equal? false)) | |
(.equals (Foo.) (Foo.) ; => false | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment