Fork me on GitHub
#clojure-uk
<
2021-04-07
>
dharrigan05:04:01

Good Morning!

3
3
seancorfield05:04:49

If y'all could change any Clojure error message to improve it, what would you change?

folcon12:04:37

One obvious thing is making eval'ed errors better? As in if I send a form to the repl I get back an error and it's sometimes really challenging working out where the error actually is? Perhaps printing out the form of the error as part of the error message? The number of times I have an error message and my immediate thought is, well that's just great, where is that coming from then! This might be a bigger issue in clojurescript where your access to a running repl isn't always available...

3
seancorfield15:04:23

@U0JUM502E I’d love to see specific examples of that.

folcon15:04:10

Well even given the examples you've shown here: https://clojurians.slack.com/archives/C064BA6G2/p1617773331132400 Contrast that with this:

: 4:11 error: cannot assign to `x` because it is borrowed [E0506]
     x += 1;
             ^~~~~~
: 3:15 note: borrow of `x` occurs here
     let y = &x;
                      ^
: 4:11 help: run `rustc --explain E0506` to see a detailed explanation
error: aborting due to previous error
From rust

folcon15:04:03

I can copy the code in the error and grep my codebase for it, when you eval a code block into the repl, you don't get anything other than relative line numbers...

folcon15:04:16

I don't know, more info that can be given is better?

folcon15:04:28

I don't know how much of this is possible, but I thought that there was some recorded metadata about the current form that was stored which could be retrieved? (could be completely misremembering that)...

folcon15:04:15

Even just better errors during eval would make things nicer for REPL experience 😃...

folcon15:04:56

Sorry I'm not just throwing together a clojure example, today isn't supposed to be a coding day for me so not in the headspace for it >_<...

seancorfield15:04:30

NP. Yeah, I think in the general case the actual form that threw the exception isn’t available to the error reporting mechanism in Clojure so that’s a bit of a non-starter.

folcon15:04:38

I thought the reader recorded all line numbers of the code that it read as metadata against the functions?

folcon15:04:11

So my thought was just lookup that and display it

folcon15:04:48

If it was recently evaled it might be more problematic, as I'm not sure how locally evaled code is assigned a "file", but they look like generated names, so I was under the impression it would be possible to map an exception's stacktrace info to that same "file" / line number...

seancorfield15:04:41

I don’t think the full file path is always available — and it’s probably not going to help in a lot of cases where the exception occurs in “other” code: the “Don’t know how to create ISeq from” exception comes from line 557 of clojure.lang.RT for example.

folcon10:04:42

When you say it's not available, is it a unique value per eval? Or nil? If it's unique that should be sufficient to work out what it came from? To be honest at this point I probably should be checking my assumptions about how that all works anyway 😃...

seancorfield16:04:42

By the time the exception is caught all you have is a stacktrace with munged class names (or classpath-relative filenames) + line numbers and the exception itself could have occurred in arbitrary code, not user code — and if it was in a thread, it’s possible that there’s no user code at all in the stack trace.

seancorfield16:04:01

There are some heuristics that can be applied to sometimes arrive at the place in user code that called whatever blew up (but if the stack includes 3rd party libraries, you can’t tell those from user code really).

folcon18:04:40

Hmm, that's not quite what I meant, what I meant is if you call meta on a var you get line info + file info. Just did this a the repl within a comment block:

(def a 1)
=> #'example.core/a
(meta #'example.core/a)
=>
{:line 21,
 :column 3,
 :file "/Users/folcon/Documents/Code/clojure/example/src/example/core.clj",
 :name a,
 :ns #object[clojure.lang.Namespace 0x65f2460b "example.core"]}
(defn mult [x] (* x x))
=> #'example.core/mult
(meta #'example.core/mult)
=>
{:arglists ([x]),
 :line 23,
 :column 3,
 :file "/Users/folcon/Documents/Code/clojure/example/src/example/core.clj",
 :name mult,
 :ns #object[clojure.lang.Namespace 0x65f2460b "example.core"]}
I didn't mean use the exception directly to get that data, I was thinking whether this could be looked up when an exception occurred so we could grab the file name and line number, look it up return the actual var/symbol and use that to get the source... But it might be a bit too complicated...

seancorfield18:04:55

There is no control over the code when an exception occurs though — only where it is caught.

seancorfield18:04:26

You can’t get at the context of the throw except via the Java stacktrace.

folcon20:04:24

A valid point.

seancorfield05:04:04

(this is spurred by the State of Clojure thread on r/Clojure)

dharrigan05:04:45

Yes, interesting. I was reading the discussion too on reddit, quite animated 🙂

dharrigan05:04:07

I will have to have a ponder

seancorfield05:04:51

I've been experimenting with a doctored REPL locally:

seanc@DESKTOP-30ICA76:~/oss$ clj -M:dev/repl
Selected port 35617 for the Socket REPL...
Starting clojure.main as the REPL...
Clojure 1.10.3
user=> (->> [1 2 3] (group-by identity) (map #(reduce + %)))
Error printing return value (ClassCastException) at clojure.lang.Numbers/add (Numbers.java:153).
class clojure.lang.PersistentVector cannot be cast to class java.lang.Number (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
(user=> @2
Execution error (ClassCastException) at user/eval271 (dev.clj:296).
class java.lang.Long cannot be cast to class java.util.concurrent.Future (java.lang.Long and java.util.concurrent.Future are in module java.base of loader 'bootstrap')
user=> (seq 2)
Execution error (IllegalArgumentException) at user/eval273 (dev.clj:1).
Don't know how to create ISeq from: java.lang.Long
user=> (1 2 3)
Execution error (ClassCastException) at user/eval275 (dev.clj:1).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')
user=> (filter [1 2 3] inc)
Error printing return value (IllegalArgumentException) at clojure.lang.RT/seqFrom (RT.java:557).
Don't know how to create ISeq from: clojure.core$inc
(user=>
seanc@DESKTOP-30ICA76:~/oss$ clojure -M:rebel:dev/repl
Selected port 43003 for the Socket REPL...
Starting Rebel Readline as the REPL...
[Rebel readline] Type :repl/help for online help info
dev=>  (->> [1 2 3] (group-by identity) (map #(reduce + %)))
Error printing return value (ClassCastException) at clojure.lang.Numbers/add (Numbers.java:153).
Expected a number, found value of class clojure.lang.PersistentVector
dev=> @2
Execution error (ClassCastException) at dev/eval5971 (dev.clj:296).
Expected something derefable, found value of class java.lang.Long
dev=> (seq 2)
Execution error (IllegalArgumentException) at dev/eval5973 (dev.clj:1).
Expected sequence, found: java.lang.Long
dev=> (1 2 3)
Execution error (ClassCastException) at dev/eval5975 (dev.clj:1).
Expected function, found value of class java.lang.Long
dev=> (filter [1 2 3] inc)
Error printing return value (IllegalArgumentException) at clojure.lang.RT/seqFrom (RT.java:557).
Expected sequence, found: clojure.core$inc
dev=>
Is that better or not?

seancorfield05:04:09

(this is literally just a regex replace on some messages -- I'm trying to gauge whether these are worth tackling at source, if possible)

dharrigan05:04:39

Certainly the removal of the (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap') is a boon. I've taught myself to just filter that out on the trace for it never showed anything of real value to me

dharrigan05:04:20

I'm still consider myself a beginner in the language, and all this about "loader 'app' and loader 'bootstrap'" is like eh?

dharrigan05:04:12

The tidy-ups you've got are a lot better in helping me understand what I've done wrong.

seancorfield05:04:18

TBH, I've gotten to the point where I translate "X cannot be cast to Y" into "expected Y, found X" anyway but I gather a lot of people find the "cannot cast" part to be confusing...

dharrigan05:04:40

definitely, esp anyone who has zero java/jvm background

seancorfield05:04:35

I got started with Java back in '97 so it's hard for me to know what is obvious and what is intimidating...

dharrigan05:04:29

The one that also is raised a lot, which I see sometimes at learner sessions (I think I saw also at clojurebridge too) is this old chestnut:

dharrigan05:04:36

user=> (defn foo [x] x)
#'user/foo
user=> (foo 1 2)
Execution error (ArityException) at user/eval24625 (REPL:1).
Wrong number of args (2) passed to: user/foo

seancorfield05:04:00

OK, I don't understand what is surprising / confusing about that?

dharrigan05:04:03

It would make sooo much more sense if it was "Wrong number of args passed to : user/foo. Expected 1 got 2"

solf06:04:45

It would need special handling for multi-arity functions though

folcon12:04:55

Not sure if that would be rejected on perf grounds?

seancorfield05:04:33

Really? OK. Good to know.

dharrigan05:04:41

Beginner's mind 🙂

seancorfield05:04:17

I'm going to try to track down some of these at source and submit patches to try and get these improved in 1.11

dharrigan05:04:07

I'm going to ponder on some more. I was writing some code yesterday that produced some stack (my errors), so will reproduce to see if there is anything obvious that would have helped me understand better the problems.

dharrigan05:04:46

There was some really good too-and-fro in the reddit discussion. Yes, some charged, but at the same time, it shows a level of passion for the language and community.

solf06:04:45

It would need special handling for multi-arity functions though

folcon12:04:37

One obvious thing is making eval'ed errors better? As in if I send a form to the repl I get back an error and it's sometimes really challenging working out where the error actually is? Perhaps printing out the form of the error as part of the error message? The number of times I have an error message and my immediate thought is, well that's just great, where is that coming from then! This might be a bigger issue in clojurescript where your access to a running repl isn't always available...

3
dharrigan13:04:27

man, it's so easy to buy on amazon

dharrigan13:04:29

click click buy

djm13:04:19

Especially for digital things

jiriknesl15:04:33

I would happily buy from alternatives, but it seems to me, there are none.

jiriknesl15:04:27

But yes, it is very easy.

mamapitufo15:04:47

I've managed to not buy anything from amazon in over a year. I guess there's always the "not buy it" alternative :P

thomas15:04:03

I'm using AWS now and it feels a bit dirty TBH.

dharrigan15:04:57

I'm curious, for anyone using clojurescript, what components do you use to make your site pretty, aka bootstrap, but clojurescript aware? I'm not totally aware of the clojure ecosystem, do I don't know what people use