3

I'm learning Clojure, and I find difficult to understand where a specific compiler error happens:

java.lang.ClassCastException: java.lang.Long cannot be cast to 
clojure.lang.IPersistentCollection, compiling:(fwpd/core.clj:100:1)

Line 100 is just:

(fib-seq3 5)

So it says nothing, because in fact the error is in the fib-seq3 function (parameters to a "conj" call are inverted, see below).

Is this normal? No way to know where an error is???

Just for reference, here's the code (again, I know where the error is; I just don't understand how was I supposed to find it, given that the message doesn't tell me at which line it happens):

(defn fib-seq3
  ([to]
   (fib-seq3 [] 0 1 0 to))
  ([coll a b k to]
    (if (= k to)
      coll
      (fib-seq3 (conj b coll) b (+ a b) (inc k) to)))

(fib-seq3 5)

2 Answers 2

3

Stack traces in Clojure suck. In fact, error messages were rated by Clojure community as the top priority area for improvements, as well as Clojure most frustrating part.

This problem is not new. There was no considerable improvements in Clojure stack traces for quite a long time. But Clojure team is fully aware of this situation, so we could hope for improvements.

To better understand Clojure stack traces try reading Clojure Stack Traces for the Uninitiated. Though the article is somewhat old, it's still relevant.

In short, you should look for so-called "cause trace", which is a second part of any Clojure stack trace and starts with "Caused by" phrase.

Sign up to request clarification or add additional context in comments.

3 Comments

my problem is not understanding the error; it's understanding where the error is. in Clojure Stack Traces for the Uninitiated he gets a proper stack trace: at stacktraces.core$make_sandwich.invoke(core.clj:4) at stacktraces.core$_main.invoke(core.clj:7) while I get the error on the calling function line... not on the called!
@Leo are you running your code in REPL? Clojure REPL usually prints only a root-cause of your exception. You could use (print-cause-trace *e) from clojure.stacktrace namespace to print full cause trace.
I strongly recommend using (.printStackTrace *e) if you're running in a repl that truncates stacktraces by default. Don't mess with print-cause-trace: every layer of tooling you insert between yourself and your stacktraces is one more thing that can go wrong, at a point when things have already gone wrong.
1

The problem is that I was using REPL (Vim+Fireplace) to execute the code. Executing using lein repl fixed the problem.

@Leonid @amalloy:

(.printStackTrace *e)

gives the proper stacktrace in the REPL (even from inside Fireplace, using "cqp" which gives the REPL prompt), so thank you very much for the comment (didn't know that!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.