Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transform for field assignments #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

athos
Copy link

@athos athos commented Jan 8, 2017

With the current implementation of +++, the result of macro expansion for field assignments looks somewhat problematic:

cljs.user=> (def obj #js{:x 0})
#'cljs.user/obj
cljs.user=> (+++ (set! (.-x obj) 42))
clojure.lang.ExceptionInfo: set! target must be a field or a symbol naming a var
 at line 1 <cljs repl> {:file "<cljs repl>", :line 1, :column 1, :root-source-in
fo {:source-type :fragment, :source-form (+++ (set! (.-x obj) 42))}, :tag :cljs/
analysis-error}
        at clojure.core$ex_info.invokeStatic(core.clj:4617)
        at clojure.core$ex_info.invoke(core.clj:4617)
        at cljs.analyzer$error.invokeStatic(analyzer.cljc:632)
...
cljs.user=> (macroexpand '(+++ (set! (.-x obj) 42)))
(do (set! (fence.core/dot obj -x) 42))
cljs.user=>

(The form (set! (fence.core/dot obj -x) 42) will in turn be expanded to (set! (aget obj "x") 42), which is a bad form.)

This PR enables +++ macro to deal with field assignment forms appropriately.

cljs.user=> (+++ (set! (.-x obj) 42))
42
cljs.user=> (macroexpand '(+++ (set! (.-x obj) 42)))
(do (clojure.core/aset obj "x" 42))
cljs.user=>

Also, a new macro fence.core/set! is provided as a shorthand for the combination of +++ and set!:

cljs.user=> (fence/set! (.-x obj) 42)
42
cljs.user=> (macroexpand-1 '(fence/set! (.-x obj) 42))
(clojure.core/aset obj "x" 42)
cljs.user=>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant