Fork me on GitHub
#clojure
<
2023-04-22
>
ericdallo14:04:41

Is this a known clojure.edn/read-string bug?

(edn/read-string "2something") => throws exception: Invalid number: 2something
(edn/read-string "something2") => works

ericdallo14:04:20

nvm, edn says it should not start with number

👍 2
skylize16:04:12

Just to remind you of the obvious, which you already know, but are probably forgetting during the context switch between a string and the edn inside that string: something2 would be a symbol, right? And symbols cannot start with numbers, so 2something is not a valid symbol. But it also is obviously not a valid number. So what should it be read as that would be valid?

ericdallo18:04:30

Yeah, indeed, the issue was a string in my code actually that later becomes a symbol and things blow up :)

Alex Sky20:04:54

(edn/read-string "\"2something\"")
?

respatialized17:04:08

When I print some objects with custom tagged literals, the tag is preserved.

(println (thi.ng.geom.rect/rect 240.298 298.29 30 30))
;; => #thi.ng.geom.types.Rect2{:p [240.298 298.29], :size [30.0 30.0]}
but when I use clojure.pprint/pprint to print them, it is not.
(clojure.pprint/pprint (thi.ng.geom.rect/rect 240.298 298.29 30 30))
;; => {:p [240.298 298.29], :size [30.0 30.0]}
is there a version of pprint that will preserve custom tagged literals the way print and pr-str will?

respatialized18:04:39

It seems like it should be possible to achieve what I'm trying to do - which is pretty print a custom record while also inserting line breaks by binding clojure.pprint/*print-right-margin* - using clojure.pprint/*print-pprint-dispatch*. But I'm not quite sure how.

hiredman18:04:45

Print uses a different system of extension

hiredman18:04:03

The dispatch stuff in the docs

hiredman18:04:04

I've never defined my own, it just ships with different dispatchers (or whatever they are called) for formatting how code is printed vs data

respatialized18:04:38

If I'm understanding you correctly, the thing to do might be to define my own dispatch for the custom Record, following this example, correct? https://stackoverflow.com/a/49387538

hiredman18:04:42

Yeah, as a start, but the body of the example there is pretty poor (just calling pr)

respatialized18:04:51

yeah, that implementation won't respect the rebound value of *print-right-margin* - it just puts everything on one line.

hiredman18:04:01

Doing that will stop any pprint specific options applying to how the intervals of the record are printed

hiredman18:04:47

What you need to do is decompose your record into parts (a tag and a map or whatever) and then as pprint to print those parts

respatialized18:04:48

yeah, I'd have to start with something like this extremely oversimplified example, I think:

(let [r (thi.ng.geom.rect/rect 240.298 298.29 30 30)
        lit (tagged-literal 'thi.ng.geom.types.Rect2 (into {} r))]
    (str "#" (:tag lit) " " (:form lit)))