r/Clojure • • Aug 11 '18

Error message improvements in Clojure 1.10

https://mobile.twitter.com/puredanger/status/1028103654241443840
89 Upvotes

41 comments sorted by

View all comments

4

u/clj_user Aug 11 '18

It looks almost exactly the same. And I'd love to see a comparison to 1.8, before spec, and before the messages got worse.

1

u/the2bears Aug 11 '18

Why wouldn't you just run the expressions used with 1.8 and post the results?

5

u/alexdmiller Aug 11 '18

Because I’m on a phone. They should all be the same other than the spec one afaik.

5

u/the2bears Aug 11 '18

/u/alexdmiller, my comment was not a reply to you, rather to /u/clj_user. They'd "love" to see the results, but not near enough to actually take 5 minutes to view them in a REPL.

3

u/alexdmiller Aug 12 '18

Ah, sorry I misread that

3

u/alexdmiller Aug 11 '18 edited Aug 11 '18

I assume the point being made here is that in 1.8 there was no spec and you instead got the hand-crafted macro-specific message about "cond requires an even number of forms". That's certainly a better message than the spec error in 1.9 or 1.10, so point taken. But it's worth unpacking why and the bigger picture.

The cond form (and other complex macros like defn, ns, etc) in core have years of pounding and tons of custom code to explicitly check for common user errors and respond appropriately (it's like writing a mini compiler). The whole point of spec is to write the spec and automatically get those checks with automated messages. The specs can then automatically catch all sorts of cases that either were silently ignored in the past or failed in strange ways with confusing errors. So while this particular case maybe is not as good, I'll still contend that the spec is doing a better job checking much more thoroughly with no macro checking code required.

In this particular case, the bindings involved use destructuring and destructuring is a recursive spec that is inherently challenging to spec and report well automatically. That said, some simple tweaks to the specs can actually encode this check more directly (https://dev.clojure.org/jira/browse/CLJ-2376) and then with the 1.10 changes you might see something like this:

user> (let [x])
Syntax error macroexpanding clojure.core/let at (69:1). Cause: Call to clojure.core/let did not conform to spec.
In: [0] val: [x] fails spec:
:clojure.core.specs.alpha/bindings at: [:args :bindings] predicate: (even? (count %))

which I think is reasonable for an automated error.