Fork me on GitHub

Is it possible to use some equivalent of `

(if (symbol? symbol-or-data) (-> symbol-or-data resolve var-get) symbol-or-data)
for macro arguments in clojurescript? The macro tests fine for clojure (as expected), but always returns nil for clojurescript (maybe expected as well).


It is possible but in a very limited use-case. What are you actually trying to do?


I'm trying to make a formula generator. The syntax is something like this:

(deff :a)
(deff :b)
(deff :c :function (* :a :b))
(def normal-fn-xyntax [map]
   (let [a (:a map) b (:b map)]
      (assoc map :c (* a b))))
;;               given   required
(def calculation (create-calculation [:a :b] [:c]))


Any firebase users here? How do I convert a Timestamp to a date in cljs?


(.fromDate js/firebase.firestore.Timestamp (js/Date.))

👍 1

that was the right answer


already use cljs-time

Michaël Salihi15:03:39

OK I see, in pure JS interop for Firebase.


is there a thing about shadowing js names? e.g fetch vs js/fetch? Is there a reason this should result in a infinite loop? (let [fetch (fn [] (js/fetch ...))] (fetch))


@drewverlee js/ is special and doesn't shadow anything, so its not protected


shadowed probably isnt the right word. When you say its not protected what does that mean?


I don't know the right word either


normally the compiler ensures it doesn't produce invalid code but js/ doesn't and may end up creating broken code


Huh. Can optimizations end up creating a local symbol that shadows a global JS variable, so that something like js/Gx ends up pointing to the local symbol?


@U2FRKM4TW i assume thats whats happening here.


@drewverlee Definitely not your case because js/fetch won't be changed by the compiler.


What I meant is:

// some_file.js
window.Gx = 42

;; some_file.cljs
(let [my-var 1]
  (js/console.log (+ my-var js/Gx)))
In this case, GCC might decide to rename my-var to Gx just as a step in the advanced optimizations. If that's the case, I can end up getting 2 instead of 43 as the result.


@U2FRKM4TW ah. Then why do you think js/fetch seems to call fetch in the example code i linked. I didnt try that snippet per say, but thats whats happening in the slightly more complex function i modified.


@U2FRKM4TW yes this is why externs is important for external JS


@drewverlee it sounds like CLJS-generated code at dev time is shadowing the fetch var


I put this code into

(let [foo 1]
  (prn js/foo))
and it emitted:
var foo_22 = (1);,foo_22);


I also get this warning: WARNING: js/foo is shadowed by a local at line 2


intresting. i might have been looking past that warning because the exception was more in my face.


the warning is in the compiler, not the runtime


@U4YGF4NGM That was not meant to be an external JS file. And would externs prevent my-var from being renamed to Gx?


externs basically tell the compiler to not use those names and not rename those that use it. so yeah externs for Gx would make closure never use that name and never change it.


Nice, thanks. And it doesn't matter whether the JS code comes from NPM or from within my own sources directory, right? Still, feels strange that one would have to write externs for something as explicit as a symbol prefixed by js/.


Or am I wrong, and using js/ does auto infer externs, and that's exactly why that example with fetch is not working, because the CLJS local was not renamed as well?


in shadow-cljs using js/ automatically adds externs for it

👍 1

So i want to make a bookmarking js for these lesson pages, I think saving the page is easy but saving the precise location on the page is a question mark. What would be a good strategy, would it be legit to somehow save the node's location in the dom tree and then scroll to that somehow?


You generally want to do that with anchor links


@lucio Aha. Hmm. Like put anchor links in the page and just reference the anchor ?


Yes. It saves you the trouble of having to manually scroll to different points on the page.


This is a good idea, can i extract the container anchor element on click?


Yes, you can simply look at the href attribute of the clicked a tag node


(-> (js/document.getElemenentById “foo”) .-href)


This gives you the full url + anchor .. if you just want the anchor you need to call (.getAttribute (js/...) “href”)


Or if it’s on click you can get there by using your event target rather than starting from js/document

Ben Hammond22:03:31

I see Circular dependence detected when trying to build a cljs project

Analyzing resources/public/example-out/cljs/core.cljc
Unexpected error (AssertionError) compiling at (REPL:1).
Assert failed: Circular dependency detected, cljs.core -> cljs.core
(every? (fn* [p1__2062#] (not (contains? *cljs-dep-set* p1__2062#))) deps)
I can also see
(ns cljs.core
  #?(:cljs (:require-macros [cljs.core :as core]
which, naively does indeed look like a circular dependency I'm running
user=> *clojure-version*
{:major 1, :minor 10, :incremental 1, :qualifier nil}
on openjdk 11.0.6 2020-01-14 and I cannot explain this; I kind of expect macros to always be in a .clj and to be (:require-macros d from a .cljs and this world of shoe-horning everything into a single .cljc confuses me. Am I barking up the wrong tree? What else could be causing my circular dependency?


Requiring the corresponding clj macros ns from a cljs ns is a nice trick that enables all users of that ns to get access to macros without having to do refer-macros . So there’s nothing wrong with this one.


@U21QNFC5C It's in a cljc file though. There's no other core file. Does the same still apply in this case?


Searched for a bit - apparently, it at least was necessary, and potentially one of the compiler's updates have made that obsolete.


@U793EL04V Why do you have the cljs/core.cljc file in resources/public/example-out? It looks like it was put there by some compilation step, and that that dir is on the classpath at the same time.


@U2FRKM4TW Hm, so you’re saying we can now write cljs code with macros in cljc, and not have to do refer-macros from code using that ns?

Ben Hammond09:03:33

its part of someone else figwheel project that I'm trying to run the demo for


@U21QNFC5C No. I'm saying that if you use just a single cljc file for both CLJ and CLJS code, then require-macros is not needed, maybe.

Ben Hammond09:03:14

I know the self-hosting macros code has been in cljs.core for several years, so it must be fine most of the time


@U793EL04V Do you have a link? If not, can you try to exclude that directory from the classpath or at least make sure core.cljc does not end in there?

Ben Hammond09:03:31

so this is the main project I am trying to run clj -A:dev:example from the uix/core directory

Ben Hammond09:03:16

I'm sure it must be something wonky in my local setup


Seems like it - that command worked fine for me.


Maybe you have something in your ~/.clojure/deps.edn?


Ah, wait. I just reproduced the issue. It was the second time running that command that set it off.


There's something strange going on. The target directory is on the classpath; the uix/core is being checked out from Git as part of the building process of... uix/core.


I would definitely file an issue or two. :)


that example build build self-hosting cljs, might be something there @U793EL04V is this piece of code important for you to get running? I don't remember if it even works

Ben Hammond11:03:27

no its not important to me

Ben Hammond11:03:47

I'm just in a discovery phase where I'm asking > why? about everything

Ben Hammond11:03:54

and I realised that what I actually wanted to run was

clj -A:dev:rec-front
and that worked fine


I'd also clean build artifacts before running another build, to avoid issues with cache