clojurescript

2025-03-19T07:59:40.065199Z

This is a really basic question... I'm trying to do (set! (.-x acc) 1), but I don't want literally acc.x, x is a parameter passed in. I understand I should not use aset for this, but I don't know the right way.

rolt 2025-03-19T08:09:19.876149Z

I still use goog.object/set (which is the same as aset really, for the generated js but at least it's not using a function in the wrong context)

2025-03-19T08:24:14.134579Z

Ah, that makes sense. I suppose here's where I mention that I'm not using official cljs but rather #squint, so the question might be better asked over there.

rolt 2025-03-19T08:29:07.288509Z

would assoc! work in squint ?

2025-03-19T08:30:35.626059Z

Looks like it does! It's specifically defined to operate on objects so it's probably the way to go.

p-himik 2025-03-19T09:08:10.271709Z

Just in case - another way of doing it in CLJS would be to use unchecked-set. While marked as internal, it's incredibly simple, has been around since forever, and is already used by other projects, so it's extremely unlikely to be changed or removed.

2025-03-19T09:16:13.656269Z

oh nice. that's actually slightly better in this case because it avoids an extra function call.

p-himik 2025-03-19T09:17:56.547269Z

I wouldn't pay attention to such trifles. :) The VM will do its thing and an extra call of a tiny function will probably not affect anything. If it's in a busy loop and you have doubts, you can always profile it.

2025-03-19T09:24:44.105229Z

stuff like that made a huge difference when I was optimizing my NES emulator. here though the win is just producing cleaner output code, which makes it easier for me to follow in the debugger

👍 2