Fork me on GitHub
#clojurescript
<
2020-11-09
>
zhuxun206:11:30

What's the idiomatic way to do x.y.z.w += 1 in clojurescript?

valtteri06:11:08

(-> x .-y .-z .-w inc)

zhuxun206:11:34

That doesn't sound right to me... inc is pure, isn't it? https://clojuredocs.org/clojure.core/inc

zhuxun206:11:02

x.y.z.w is going to be left unchanged

valtteri06:11:07

Ah sorry you wanted to mutate the value?

zhuxun206:11:31

I know, but it's going to be (set! (-> x .-y .-z .-w) (inc (-> x .-y .-z .-w))) which seems kind of mouthful

zhuxun206:11:19

I looked and there doesn't seem to be anything like swap! but for properties

valtteri06:11:39

Mutating properties is probably supposed to be ugly. 😄 I guess that’s what you have. :thinking_face: If you need to do that often, perhaps you could write your own helper macro or function

lassemaatta06:11:29

can you grab the reference to .w (or .z ) in a let binding and set! that?

zhuxun206:11:59

@valtteri Yeah, that makes sense

zhuxun206:11:20

@lasse.maatta Seems it would suffice for my case

mhuebert10:11:39

@zhuxun2 if you are ok with using a library, js-interop has an update-in! macro that would look like (j/update-in! x [.-y .-z .-w] inc) or if the keys are static, (j/update-in! x [:y :z :w] inc) (https://github.com/applied-science/js-interop)

👍 6
borkdude19:11:58

We have a very strange bug in our advanced CLJS compiled front-end app. We have some specs referring to other specs:

(s/def :foo/foo string?)
(s/def :bar/bar :foo/foo)
All of a sudden this breaks our app:
alpha.cljs:121 Uncaught Error: Unable to resolve spec: :foo/foo
    at u2a (alpha.cljs:121)
    at RY (alpha.cljs:518)
    at tY (alpha.cljs:513)
    at BY (alpha.cljs:314)
    at query.cljs:41

borkdude19:11:23

CLJS version 1.10.764

borkdude19:11:37

Probably it's better to not load spec at all in our advanced CLJS app

borkdude19:11:53

we're not instrumenting anything anyway

darwin20:11:12

compile it with :pseudo-names true, it could tell more

borkdude20:11:39

it just points at spec-impl where it tries to resolve that other spec by name.

borkdude20:11:52

which it can't find, while it was defined just before.

borkdude20:11:57

Repro or it didn't happen, I know. It was introduced by a non-related commit in our codebase, so hard to reproduce probably.

darwin20:11:26

with pseudo names, you should be able to debug it in chrome devtools even without source maps, just step through formatted js code

darwin20:11:05

with help of (js-debugger) you can pin-point interesting places

darwin20:11:52

just by chance, aren’t you using multiple javascript contexts? e.g. a webworker and the main page?

borkdude20:11:36

I think we might have hit some heissenbug. Requiring this namespace earlier in our app seems to get rid of this issue. But still very weird, since these specs are in the same file...

borkdude20:11:04

we're not using webworkers. anyway, we're getting rid of these specs anyway (in production at least)

lilactown23:11:06

Are you requiring the namespace that :foo/foo is defd in the namespace that :bar/bar is defd in?

lilactown23:11:29

It sounds like you’re not

lilactown23:11:12

When compiled, namespaces are ordered topologically according to who requires who. If you don’t require a namespace, but rely on some global state to be setup in it (like a spec) then there can be orderings where that state isn’t initialized when the code runs