Fork me on GitHub
#cljs-dev
<
2024-01-23
>
souenzzo13:01:39

Hello. I'd like to raise this question to clojure cljs team: What is the idiomatic way to access a JS field ? • aget works, but its docstring says that is specific for arrays • unchecked-get works, but its docstring says that it is internal. Also, it is not a function (can't apply unchecked-get .. ) In the thread, thheller talked about "update unchecked-get docstring and give a functional treatment", that I agree that could be a simple solution for this problem. See original thread https://clojurians.slack.com/archives/C03S1L9DN/p1706014470056979

p-himik13:01:59

In what circumstances would you possibly want to apply a getter like that?

souenzzo13:01:48

users inputs a JSON payload and writes a JSON pointer then call (apply unchecked-get (js/JSON.parse json-input) (parse-json-pointer pointer-input))

p-himik13:01:29

I don't understand. Does parse-json-pointer return a collection of keys? Do you expect an object getter to work like get-in but without an extra vector?

👌 1
borkdude13:01:39

I'd go for unchecked-get . You could do this:

cljs.user=> (js/Reflect.get #js {:a 1} "a")
1
but probably less performant.

👍 1
borkdude13:01:05

If you (for some reason) need unchecked-get with apply, just wrap it in a function

borkdude13:01:27

o wait, that doesn't work since the field must be known at compile time

thheller13:01:02

by "function treatment" I mean adding a defn unchecked-get to cljs.core CLJS side, which then just delegates to the macro. just so that apply works. the macro stays as is. this trick is used many more times in cljs.core

borkdude13:01:39

I was wrong about compile time, this just works:

cljs.user=> (defn ah-get [obj fld] (unchecked-get obj fld))
#'cljs.user/ah-get
cljs.user=> (ah-get #js {:a 1} "a")
1

👍 1
souenzzo13:01:00

@U2FRKM4TW yes. but I'm thinking in "work the same as aget but objects" @U04V15CAJ I'm currently using this.

thheller13:01:29

and don't use Reflect. thing[foo] is perfectly fine to do in JS and that is what unchecked-get provides

p-himik13:01:06

> I'm thinking in "work the same as aget but objects" aget is a macro though. :)

borkdude13:01:36

try (str aset) and you'll see there is a runtime function for it as well

thheller13:01:39

aget is also a function, which is the trick I'm referring to ...

p-himik13:01:53

Ah, shoot, right.

p-himik13:01:27

Still get occasionally burned by the fact that CLJS is in both .cljs and .cljc when reading the code.

dnolen14:01:00

@souenzzo there’s really no point in avoiding gobj/get

dnolen14:01:18

assume it will work for at least another 10 years

dnolen14:01:18

we will probably eventually replace the various GCL namespaces since most of them are simple - but breaking stuff has no benefit - this ain’t the JS ecosystem - there’s no value in breaking stuff

1
dpsutton20:01:47

small note: Post announces 1.11.132 but lists contributors as having contributed to 1.11.51

dnolen20:01:29

fixed thanks 🙏:skin-tone-4:

👍 1
jpmonettas21:01:36

small typo in "Note that Google stopped provided regular releases many years ago"

hifumi12321:01:36

similarly, > Google is not going to remove Google Closure Library (GCL) or remove the API docs or doing anything that would be detrimental to ClojureScript. should be revised like so > Google is not going to remove Google Closure Library (GCL), remove the API docs, or do anything that would be detrimental to ClojureScript.

dnolen23:01:03

thanks! fixed

dnolen20:01:11

release forthcoming, feedback welcome before I publish this post tomorrow - this also addresses the worry around GCL

👍 1
dnolen20:01:35

the release itself will be out soon (triggered a build just a little while ago)