r/Clojure • • Jan 19 '14

Clojure, the bad first impressions

http://kodare.net/blog/clojure-bad-first-impressions/
70 Upvotes

81 comments sorted by

View all comments

17

u/yogthos Jan 19 '14

I think the error reporting in Clojure core functions definitely leaves a lot to be desired. For example, let's say we pass a wrong type to +, we'll get the following error:

(+ 1 "foo")
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number

Why can't it say something like:

second argument to + was "foo", a number is expected

While, it's obvious what's going on to somebody who's got experience with the language, it's very daunting for beginners.

1

u/Anderkent Jan 20 '14
(+ 1 "foo")
ClassCastException java.lang.String cannot be cast to java.lang.Number  clojure.lang.Numbers.add (Numbers.java:126)

The clojure.lang.Numbers.add should help.

As to why it can't be nicer - it could in this specific case, but a general solution is hard. Numbers.java could typecheck args before casting (or catch ClassCastExceptions)... But that's still not super useful if you don't know how exactly the function was called.

2

u/yogthos Jan 20 '14

I'm mainly saying that it could print the argument that it got in the error message. It's already there, and the function name should be available as well as it's just a symbol.

Again, we know what function was called, because it was called as + and not as clojure.lang.Numbers.add, all the information is there, it's just not being shown in a friendly way.

1

u/Anderkent Jan 20 '14

This information is out there somewhere, but it's several layers of indirection between it and where the actual problem occurs. Now, I suppose you could do stack trace inspection and thus derive what actually called you, what args were given to it, etc. But it's not simple, writing it separately for every core function would be insanity, and a generic solution is even harder.

3

u/yogthos Jan 20 '14

So, just to be clear what I'm proposing is this:

  • the function prints its own name in the error message

  • the function prints out the parameters that it sees on the stack

That's all we're talking about here. I see no reason why this information can't be captured and displayed in a generic fashion when an exception occurs.One approach is to simply catch the exception at the call site and inject the information there, I'm sure a more efficient way is possible though.

1

u/Anderkent Jan 21 '14

Ah, but which function? The one that throws the exception? Then you'd see clojure.lang.Numbers.add 1 "foo". Which, sure, is somewhat useful, but adding a try catch to every single function out there is a pain.

The lowest level clojure function in core? Again, either have to manually add the same codeblock in every function (luckily with macros that might be more manageable - defcorefn or something that catches ClassCastExceptions) - but you have to check where exactly the classcast is happening (could be that the exception is unrelated to the args), the java / clojure boundary is somewhat arbitrary at times, and it all seems pretty messy.

The problem is that any 'generic' exception you see (NPE, ClassCast etc) might come from the clojure code, but might code from some underlying java lib. You want 'prettified' output in the first case, but still the original output in the second. I guess clojure could switch to its own exception types that know how to render themselves... But again, it's a large change in many places, not a generic solution.

4

u/yogthos Jan 21 '14

I agree that it would be a large change, but it certainly would be very helpful to users.

A proper solution is obviously not trivial, but it's certainly is not intractable either. This is probably one of the most common time wasters for the developer, as you see a lot of silly errors all the time and you have to spend those extra seconds parsing them in your head.

I think a good example of the impact is Clang vs. GCC error reporting. Right now, Clojure reports errors like GCC and it causes pain and frustration to all the users on daily basis.