Skip to content

Instantly share code, notes, and snippets.

@lambdahands
Created November 5, 2013 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdahands/7326241 to your computer and use it in GitHub Desktop.
Save lambdahands/7326241 to your computer and use it in GitHub Desktop.
Using bound vars as argument to `to-attr` in macro produces no results. Not sure what mistake I'm making.
(ns my-project.core
(:require-macros [my-project.macros :refer [style-el]]))
(defn create-el [kind]
(. js/document (createElement (name kind))))
(let [div (create-el :div)]
;; Works as expected
(style-el div :width "50%")
;; Produces no results
(doseq [[x y] {:width "25%" :color "#fff"}]
(style-el div x y)))
(ns my-project.macros)
(defn to-attr [attr]
(symbol (str ".-" (name attr))))
(defmacro style-el [el kwd value]
`(set! (-> ~el .-style ~@(to-attr kwd)) ~value))
@lambdahands
Copy link
Author

Ended up taking some clues from the domina library. I was pretty set on rolling my own methods for the sake of learning, but attempting to apply any sort of JavaScript mindset while programming ClojureScript is an unproductive and frustrating venture.

I've settled with using Google's Closure API, which takes most of the headache out of the equation. Maybe later on in my experience, I'll find a way to solve this particular problem.

@swannodette
Copy link

Yes the problem here is that you're trying to have runtime code interact with macro code which really isn't possible. style-el is probably just best written as a plain function.

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