This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-13
Channels
- # announcements (2)
- # babashka (30)
- # beginners (44)
- # biff (20)
- # calva (15)
- # cider (7)
- # clerk (4)
- # clojure (15)
- # clojure-austin (1)
- # clojure-europe (35)
- # clojure-hungary (1)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (7)
- # clojurescript (33)
- # conjure (1)
- # cryogen (1)
- # cursive (3)
- # data-science (8)
- # docker (2)
- # emacs (78)
- # gratitude (2)
- # hyperfiddle (26)
- # improve-getting-started (1)
- # jobs-discuss (12)
- # lsp (7)
- # malli (11)
- # missionary (57)
- # off-topic (14)
- # pathom (13)
- # portal (6)
- # reagent (6)
- # releases (3)
- # ring (25)
- # shadow-cljs (18)
- # squint (11)
- # vim (3)
is there a way to force evaluate a macro parameter before doing macroexpand?
i want to do (j/lit (mapv inc [1 2 3]))
but the j/lit
is a macro
(https://github.com/applied-science/js-interop)
(macroexpand '(j/lit (mapv inc [1 2 3])))
(mapv inc [1 2 3])
this will only get the cljs regular vector.
how do i fix this issues
(i know i can use clj->js
to convert, just wonder what people normally do when they want to evaluate a thing before sending to a macro)
thanks!
A big part of a macro being a macro is that it doesn't evaluate its arguments...
(i know i can use clj->js to convert, just wonder what people normally do when they want to evaluate a thing before sending to a macro)Generally speaking, you don't want to evaluate forms before sending them to a macro. In many cases, you can just have your macro work the with form, eg:
(defmacro my-macro [form]
`(let [val# ~form]
(do-something val#)))
There are a few cases where you want to do some extra work at runtime, but it depends on the use case and it's hard to give general advice without knowing more about why you might be interested in evaluating arguments to a macro.@U060QKK2P8V @U7RJTCH6J thanks for all all your replies!
this is my use case:
let's say i have a js obj, which has an "js array" in obj.a.aa,
(i construct it by (j/lit {:a {:aa [1 2 3 4]}})
in this example)
(j/update-in! (j/lit {:a {:aa [1 2 3 4]}}) [:a :aa] (fn [x] (clj->js (mapv inc x))))
the above code achieve what i want, however this doesn't:
(j/update-in! (j/lit {:a {:aa [1 2 3 4]}}) [:a :aa] (fn [x] (j/lit (mapv inc x))))
because j/lit
is a macroWhat does j/lit
do?
and does it need to be a macro, in the first place?
j/lit
returns literal js objects/arrays for an arbitrarily nested structure of maps/vectors
from https://github.com/applied-science/js-interop
If the work can be done at runtime compile time (see correction below), then j/lit
can help, but if the value is only available at runtime, then a macro has little benefit.
whoops. yea.
Hey! I'm trying to write a macro for instrumentation purposes, it's similar to with-open
in Clojure but I'm not quite getting it right... 🧵
(defmacro with-span
"Wrap the body in a tracing span identified by `ident`.
If body returns a promise, this will return a promise as well."
[[ident opts] & body]
`(do
(start-span* ~ident ~opts)
(let [result# (do ~@body)]
(if (p/promise? result#)
(p/then result#
(fn [x#]
(finish-span* ~ident {})
x#))
(do (finish-span* ~ident {})
result#)))))
This is my current version and I can call it from Clojurescript but there's some issues:
1. When calling (with-span [:foo :bar] (p/delay 1000))
it looks like ident, opts and body are all nil
2. Calling the macro in a REPL does not actually evaluate the code but returns it as a quoted list (it seems), maybe this is normal? :thinking_face: I tried getting some help by ChatGPT, which seemed helpful at first but ultimately told me to come here: > If you continue to experience issues, it might be helpful to create a minimal reproducible example that isolates the problem, which you can use to seek further assistance from the ClojureScript community or on forums like ClojureVerse or the Clojurians Slack. 😅
I was trying to use that blogpost as a guide, after moving the macro into a separate .clj file it seems to work as expected 🙂 thank you for writing it!
why is https://github.com/clojure/clojurescript/blob/v1.11/src/main/cljs/cljs/core.cljs#L1332 experimental? What does it means? It can be removed? change?!
I guess it means it was added quickly and never revisited since then.
Nevertheless it works well
Maybe related ticket: • https://clojure.atlassian.net/browse/CLJS-2527 Just log here. I'm preparing to raise it to #C07UQ678E
@souenzzo at the time it was added it was unclear how useful it would be (and it was specific to ClojureScript), ES6 adoption wasn’t so pervasisve - we can probably remove the EXPERIMENTAL bit now that so much time has passed
FWIW, I'd prefer for the ES6 functions to stay.
Some JS APIs use the ES6 iterators (e.g. URLSearchParams.entries
), GCL uses them at least in goog.iter
.
hi, I am learning Clojurescript and i'm new to js. So I have no idea what properties like .-onkeyup
, .-keyholder
means, along with other js based functions such as .getElementById
where can i learn all of this from. Can you point me to some resources?