Skip to content

Instantly share code, notes, and snippets.

@tdavis

tdavis/ugh.clj Secret

Created February 11, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tdavis/9d8b0f704f1a05f22d0b to your computer and use it in GitHub Desktop.
Save tdavis/9d8b0f704f1a05f22d0b to your computer and use it in GitHub Desktop.
(ns test)
(defn field [f fwd]
(if (= :MAGIC f)
(schema.core/maybe (schema.core/recursive (intern *ns* fwd)))
f))
(defmacro clj-structs [name fields]
(let [sym# (symbol name)
fwd# (symbol (str "fwd-" name))]
`(do
(declare ~fwd#)
(schema.core/defrecord ~sym#
~(vec (reduce concat (map (fn [[k v]] (list (symbol k) :- (field v fwd#))) fields))))
(def ^:private ~fwd# ~sym#))))
(macroexpand '(clj-structs "Thing" {"id" schema.core/Int "other" :MAGIC}))
@tdavis
Copy link
Author

tdavis commented Feb 11, 2016

macroexpand with (intern...):

(do (clojure.core/declare fwd-Thing) 
  (schema.core/defrecord Thing [id :- schema.core/Int other :- (maybe (recursive (var test/fwd-Thing)))]) 
  (def fwd-Thing Thing))

error with `#'~f:

 java.lang.RuntimeException: Not an IDeref: (var fwd-Thing)

(that recursive function makes sure the argument implements IDeref)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment