Fork me on GitHub
#clojurescript
<
2021-11-23
>
roelof11:11:01

but when I do lein figweel I see this error

Exception in thread "main" java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter, compiling:(cljs/closure.clj:1:1)

lassemaatta11:11:22

just a guess, but that looks like an old tutorial (2016) and I'd imagine that it uses dependencies which are not compatible with Java 11+. If I recall correctly, some java ee packages (like javax.* ) were separated into other packages from the JDK with Java 11, which caused this kind of errors. Perhaps try a newer tutorial or try with Java 8?

Stuart11:11:28

Looks like that uses figwheel. I think you probably want to be using figwheel main nowadays.

roelof11:11:00

oke, and how can i do that @U013YN3T4DA

Stuart11:11:15

https://figwheel.org/#quick-usage i think hte figwheel main docs are pretty good starting point

wombawomba13:11:04

How can I translate the JavaScript regex /[^]/ (matches any character including newline) to ClojureScript? #"[^]" fails with "Unclosed character class".

wombawomba14:11:05

(`#"(?:.|\n)"` works and should be equivalent, but it's a bit harder to read and possibly less performant, so I'd like to avoid it)

Colin P. Hill14:11:16

Inside a character class, the caret has special meaning: it inverts the class, so that [^abc] will match any character except a, b, and c.

Colin P. Hill14:11:36

I'm actually not sure why the JS one compiles. Any character except...what?

Colin P. Hill14:11:08

I think the usual way to match everything is to use . with the flag that enables newline matching

Colin P. Hill14:11:58

...okay I'm catching up now and realize you knew that about character classes, and you want to invert the empty set. Sorry, need more caffeine I guess.

Colin P. Hill14:11:42

Looks like flags get set at the beginning. Try #"(?s)."?

wombawomba15:11:30

@U029J729MUP that did the trick! Thanks 🙂

🥳 1
Pepijn de Vos14:11:50

Is there a way to do a binary search in a sorted vector? Like Python's bisect

Alex Miller (Clojure team)14:11:12

you could use java.util.Collections/binarySearch

Alex Miller (Clojure team)14:11:44

oh sorry, we're in cljs :)

Pepijn de Vos14:11:03

I just stole bisect-left from python

(defn bisect-left [a x]
   (loop [lo 0 hi (count a)]
     (if (< lo hi)
       (let [mid (quot (+ lo hi) 2)]
         (if (< (compare (get a mid) x) 0)
           (recur (inc mid) hi)
           (recur lo mid)))
       lo)))

Karl Xaver14:11:13

Will using dot-access like (.addListener browser/browserAction.onClicked ...) instead of something like (.. browser/browserAction -onClicked addListener ...) get me into trouble / is it very unidiomatic? * browser being the webextension-polyfill library, installed in a shadow-cljs project via npm Or does it not matter, because I should be using goog, cljs-oops, cljs-bean etc. for interop anyways, because the name-shortening feature of advanced compilation will bite me sooner or later? I went through a couple of blog posts and forum threads, but am left confused to what the "state-of-the-art" approach to interop is in 2021. Henry Widd writes in a post from this year: > Thomas Heller pointed out to me that if Google Closure did become able to optimise regular JS libraries (things that are now foreign) at some point in the future, then anything that is using strings to access JS APIs ( js-interop lib, goog.get & etc) would then be broken, so that’s something else to consider. https://widdindustries.com/clojurescript-jsinterop/ Thanks!

p-himik15:11:31

Regular interop via the dot forms and externs inference is the way, I'd say. So if you have something like (.-someField some-js-obj), just add ^js there as in (.-someField ^js some-js-obj) (or better yet, add it right where you define some-js-obj) and then you'll never have any problems with that particular some-js-obj and all the names that are mentioned in the context of its dot forms.

p-himik15:11:22

And both of your forms above are just fine, except that they should use ^js . Those forms are completely equivalent because .. is simply a macro that gets expanded into nested dot forms.

Karl Xaver15:11:45

It's calming to know there's a clear path. Thank you! Should ^js be used no matter where the object is coming from (e.g. a node lib, the DOM, a browser API) or only for external libs? I have to read up what the Closure compiler actually does here... sorry for that uninformed follow-up question.

p-himik16:11:32

I use only shadow-cljs so some of what I say might potentially be not applicable to other tools. If you use js/... (as in js/window or something else) then you don't need ^js. If you used ^js on a "parent", then all "children" will also be affected (only 90% sure here - I think I even checked that mentioned "children" fields are not minified, but for some reason now I doubt myself a bit). Not sure about the browser/node API, but using ^js cannot hurt here. And it definitely should be used with NPM libraries. However, don't use ^js for goog stuff.

thheller18:11:57

the only downside of using ^js incorrectly is that something might not get renamed that otherwise might be

thheller18:11:18

but generally only use it for stuff you get actual inference warnings for

Eddie17:11:19

I am trying to write a macro in a .cljc file, and I am struggling to get it to work in a Clojurescript environment. The macro is intended to wrap a form in a try-catch that will re-throw the error with additional data using ex-info. I have a simplified implementation below.

clojure
(defmacro with-error-context
  [context form]
  `(try
     ~form
     (catch #?(:clj java.lang.Exception :cljs js/Error) e#
       (throw (ex-info "Uh-oh!" context e#)))))
My understanding is that the macro-expansion stage happens before any compilation to JS is performed. It appears that the reader will take the :clj branch during this first stage and thus the expanded code will contain java.lang.Exception. I was surprised and confused by this, but after more thought I think I see why it has to be this way. My question is: how can I make a macro produce different code when expanded into CLJ vs CLJS?

p-himik17:11:20

(Also check out the next gotcha)

Eddie17:11:57

I was reading these gotchas last night but I totally misunderstood the point of #4. Thanks!

Eddie17:11:10

It is a little confusing to me that (:ns &env) _;; :ns only exists in CLJS_ is the logic for knowing which environment we are in. It seems like we are relying on an implementation detail for something it was not meant to be used for. But if that is the community standard then I guess it’s fine.

p-himik17:11:15

I wouldn't go as far as to call this a community standard - there really is no other feasible way. I think there was an issue about it but I couldn't find it.

Eddie17:11:17

Given this community’s history with stability, I’m not too worried about breakage.

Eddie17:11:59

I am slightly worried about collaborators being confused when reading the code. I may expand on the comment a bit to explain why reader tags :clj(s) don’t apply in macros.

p-himik17:11:14

Or just add an extra self-descriptive macro: https://stackoverflow.com/a/28759406/564509 This way, it's not only clear, it will also be much easier to start using a better detection once it's available.

Eddie17:11:56

I like that. Thanks!

👍 1