Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Last active October 20, 2015 17:45
Show Gist options
  • Save patrickgombert/2e83b0acf390e0d7485b to your computer and use it in GitHub Desktop.
Save patrickgombert/2e83b0acf390e0d7485b to your computer and use it in GitHub Desktop.
deftype with class names as vars and macroexpansion
(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