This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-02
Channels
- # adventofcode (20)
- # bangalore-clj (14)
- # beginners (72)
- # cider (2)
- # clara (2)
- # cljs-dev (8)
- # clojure (36)
- # clojure-brasil (201)
- # clojure-greece (29)
- # clojure-nl (1)
- # clojure-poland (1)
- # clojure-russia (2)
- # clojure-spec (5)
- # clojure-uk (4)
- # clojurescript (41)
- # cursive (1)
- # datomic (1)
- # emacs (6)
- # fulcro (80)
- # graphql (1)
- # klipse (2)
- # leiningen (5)
- # lumo (15)
- # off-topic (1)
- # om (3)
- # om-next (3)
- # re-frame (19)
- # reagent (7)
- # test-check (1)
- # uncomplicate (2)
- # yada (8)
What is the correct and most appropriate way to implement a multi arity macro in CLJS?
that's not the right way to implement multi-arity anything though
(defmacro foo ([arg] ...) ([arg1 arg2] ...))
I tried that, but the application of that macro says that I pass incorrect number of arguments.
(defmacro path-sub
([id path]
(path-sub id nil path))
([id input-signal path]
(let [sub-fn `(fn ~(sub-fn-name id) ~'[db _]
(~(if (vector? path) get-in get)
~'db ~path))]
(if input-signal
`(reg-sub ~id :<- ~input-signal ~sub-fn)
`(reg-sub ~id ~sub-fn)))))
9 (sm/path-sub ::scores-panel :scores-panel)
^--- Wrong number of args (1) passed to: subs/path-sub
when wrong-number doesn't even match the number passed in, that means that it's being called as a function not a macro
usually it's off by two though
path-sub
is defined in a clj
file, the error is about cljs
file, and I don't have path-sub
function.
@p-himik You can't call a macro like that. Remember, once in the macro you're running clojure code again.
it really got 3, complains about 1
@rauh just tried that:
Failed to compile "frontend/static/js/mxc_main.js" in 0.726 seconds.
---- Could not Compile frontend/cljs/musicxmlcheck/scores/subs.cljs ----
failed compiling file:frontend/cljs/musicxmlcheck/scores/subs.cljs
---- Compiler Error : Please see frontend/cljs/musicxmlcheck/scores/subs.cljs ----
And that's it, no other messages.@rauh that same issue happens in clj - a macro must return the form it emits
(macroexpand '(musicxmlcheck.subs/path-sub :a :b))
clojure.lang.ExceptionInfo: failed compiling constant: clojure.core$get@6015df86; class clojure.core$get is not a valid ClojureScript constant. {:constant #object[clojure.core$get 0x6015df86 "clojure.core$get@6015df86"], :type clojure.core$get}
Right. Also if you want to know why the original error message is about arities: A macro is a special (but still a normal) function that also takes the environment as the first argument explicitly. When you "invoke" a macro the CLJS/CLJ compiler will automatically provide it with &env
. But if you call it you'd have to do it yourself.
this is what I meant when I said the macro got invoked as a function
Thought, people usually don't do such hacky things and usually write a function that emits the code. And the macro just does one thing; Call that function. It's a common pattern.
the off-by-two is because of &env and &form, the implicit args the compiler passes to the macro
@noisesmith Yeah you're right.
I see. Now I understand https://stackoverflow.com/questions/25566146/multiple-arity-in-defmacro-of-clojure better.
I tried the approach in the SO answer and the approach with anther function, but got stuck on that unquoted get
- couldn't figure out what's wrong.
A message "failed compiling %file%" without any indication of the exact place and/or reason it not fun.
it's almost a sure thing, if you ever see "wrong number of args, -1" or if you see it complain about an arg count that is exactly two less than the actual count, a macro got called as if it were a function somewhere (usually because some other macro is broken)
Is a form prefixed with #_
still read by the reader?
I prefixed a form with #_
and all :require
's that were used only in that form, including a namespace alias for one of the keywords there. And now I get an error about an invalid keyword ::mdb/db
that's located inside the form escaped with #_
.
Found the answer in the archives. The form is still read, but no result is returned by the reader.
Hi! I'm trying to use npm-deps in cljs "1.9.908". Added :npm-deps and :install-deps to compiler options, module seems to be installed (it's there in node_modules dir). But I can't figure out how to require it... Relevant bits:
(task-options!
cljs {:compiler-options
{:install-deps true
:npm-deps
{:rethinkdb "2.3.3"}}})
cljs.user> (require '[rethinkdb :as r])
clojure.lang.ExceptionInfo: No such namespace: rethinkdb, could not locate rethinkdb.cljs, rethinkdb.cljc, or JavaScript source providing "rethinkdb" at line 1 <cljs repl> {:file "<cljs repl>", :line 1, :column 1, :root-source-info {:source-type :fragment, :source-form (require (quote [rethinkdb :as r]))}, :tag :cljs/analysis-error}
at clojure.core$ex_info.invokeStatic(core.clj:4617)
--- cut ---
cljs.user> (require '["rethinkdb" :as r])
java.lang.AssertionError: Assert failed: cljs.analyzer/foreign-dep? expected symbol got "rethinkdb"
(symbol? dep)
--- cut ---
cljs.user> (.require js/goog "rethinkdb")
#object[Error Error: goog.require could not find: rethinkdb]
Error: goog.require could not find: rethinkdb
--- cut ---
I have:
(defn rect-new [s]
(let [elem (.createElementNS js/document "" "rect")]
(.setAttributeNS elem nil "x" (::x s))
(.setAttributeNS elem nil "y" (::y s))
(.setAttributeNS elem nil "width" (::width s))
(.setAttributeNS elem nil "height" (::height s))
(.setAttributeNS elem nil "style" (str "fill: " (::fill s)))
elem))
and I'm looking at:
https://google.github.io/closure-library/api/goog.dom.html
What is the best way to convert above code to goog/dom ?I tried to use this library through the new npm features but had all kind of errors that I didn't manage to solve, even by upgrading to the latest master... https://www.npmjs.com/package/react-stockcharts
So I wonder, is there a way to use it from CLJS by just somehow including it and adding externs as necessary?
Reading this now: http://blob.tomerweller.com/reagent-import-react-components-from-npm