cljs-dev

ClojureScript compiler & std lib dev, https://clojurescript.org/community/dev
thheller 2020-09-04T06:48:41.051600Z

or just don't use tagged literals in code and you don't have to worry about it 😉

dominicm 2020-09-04T07:10:55.052300Z

In either case, still unclear how to do that in a cljc-friendly way.

thheller 2020-09-04T07:32:05.054500Z

it works basically the same as in clojure and you need to be kinda careful what you return in the reader when used in code. when used in the regular EDN reader or so it doesn't matter much since you just get whatever you returned. when used in code the compiler needs to be able to emit code that is somehow able to reconstruct the returned object. in clojure thats done via print-dup I believe and in CLJS you implement the multi-method you linked.

thheller 2020-09-04T07:32:30.054800Z

thus ... don't use it in code since thats not always practical. just calling (whatever/foo 1 2 3) is pretty much always better than #whatever/foo [1 2 3]. certainly less headaches 😛

thheller 2020-09-04T07:35:37.055600Z

IIRC there is some special support for records but thats about it

slipset 2020-09-04T07:43:26.056200Z

On the finer points of tag-handlers and time https://clojure.atlassian.net/browse/CLJS-3272?jql=project%20%3D%20CLJS

slipset 2020-09-04T07:44:19.056500Z

and the fact that they're read with java and used in js.

mfikes 2020-09-04T11:15:18.057400Z

@dominicm An example of a working CLJC-based tagged-literal project that spans Clojure and both JVM and self-hosted ClojureScript is https://github.com/mfikes/precise

dominicm 2020-09-04T11:20:14.058800Z

@mfikes that has the same problem I am trying to overcome on the JVM:

user=> (read-string "#exact/ratio \"2/3\"")
(com.gfredericks.exact// (com.gfredericks.exact/string->integer "2") (com.gfredericks.exact/string->integer "3"))
It returns code instead of objects. Compare with, e.g. uuid:
user=> (read-string "#uuid \"bdd2c05b-91a3-4e67-83ca-dcbd76ebe44c\"")
#uuid "bdd2c05b-91a3-4e67-83ca-dcbd76ebe44c"

dominicm 2020-09-04T11:21:08.059400Z

It seems to me that libraries need to return code for clojurescript, and objects for clj, but they have no way to distinguish.

mfikes 2020-09-04T11:22:48.059800Z

Oh, that's interesting. I wasn't aware that read-string behaved that way.

mfikes 2020-09-04T11:32:04.060600Z

I bet you need to bind *data-readers* when using read-string, but it has been a while since I've been down in that code

dominicm 2020-09-04T11:59:30.061200Z

read-string uses default-data-readers too

mfikes 2020-09-04T12:03:17.062100Z

Yeah, there is something subtle that is making inst and uuid behave differently, it seems. Hrm.

thheller 2020-09-04T12:13:04.062800Z

hmm why do I get a 404 on that link?

thheller 2020-09-04T12:13:42.063600Z

the issue is simply that the exact/ratio returns a code form which when encountered by the compiler will simply compile as regular code

dominicm 2020-09-04T12:13:58.064100Z

I am too

thheller 2020-09-04T12:14:21.064600Z

might be a bug if clojure doesn't do that. read-string however doesn't eval so totally normal to get the returned "code" as data

thheller 2020-09-04T12:14:51.065300Z

doing it in code does require implementing the multimethod

dominicm 2020-09-04T12:15:22.066Z

I'm pretty sure that you're not intended to return a code form from data readers.

dominicm 2020-09-04T12:16:16.066600Z

Not clear from the docs

thheller 2020-09-04T12:16:45.067300Z

yes. the question is whether clojure does indead handle it as basically a quoted list or also compiles it happily

alexmiller 2020-09-04T12:19:54.068500Z

clojure expects a tagged reader function to return an object

👍 1
bronsa 2020-09-04T12:28:00.069Z

how so?

bronsa 2020-09-04T12:29:10.069400Z

it seems totally acceptable to me to return a quoted form from a tagged reader

bronsa 2020-09-04T12:29:25.069700Z

if embedded in code it will be evaluated normally

bronsa 2020-09-04T12:30:48.070Z

just like syntax-quote returns code

bronsa 2020-09-04T12:32:35.070900Z

to me returning an object vs code seem to be both valid, different, usages of tagged reader and i can't see why one would be expected over the other

bronsa 2020-09-04T12:34:08.071800Z

similar to how both (defmacro x [] (Object.)) and

(defmacro y [] `(Object.))
are ok

bronsa 2020-09-04T12:35:26.072800Z

the difference to me seems in what the usage of a tagged literal is supposed to be, if to be embedded in code, returning code is ok since it will be evaluated, if just for reading then obviously not

alexmiller 2020-09-04T12:35:48.073200Z

yeah

alexmiller 2020-09-04T12:36:22.073800Z

I think the issue of whether you expect it to be eval'ed is important

bronsa 2020-09-04T12:36:43.074500Z

the thing in cljs vs clj is that in clj objects are self-evaluating, in cljs only if the compiler knows how to

alexmiller 2020-09-04T12:36:44.074700Z

if you're just calling read-string or something, then that's just read

bronsa 2020-09-04T12:37:04.075200Z

but even in clj, it will only self-evaluate when running through the repl, if you try to embed them and AOT it may explode if there's no print-dup

dominicm 2020-09-04T16:53:56.079400Z

Unfortunately, that emit-constant* is not part of the public compiler API. There's no way in user space to define reader literals which work with read-string and also in clojurescript.

2020-09-04T17:04:58.079500Z

Well technically cljs is just a Clojure library, so everything is possible :)

🦜 1
scknkkrer 2020-09-04T17:47:31.080Z

God, I love this language.