Welcome! Please see the About page for a little more info on how this works.

+5 votes
in Errors by

I accidentally misplaced the args to nth and was presented with the following error and stacktrace:

  Show: Project-Only All 
  Hide: Clojure Java REPL Tooling Duplicates  (0 frames hidden)

2. Unhandled clojure.lang.Compiler$CompilerException
   Error compiling NO_SOURCE_FILE at (79:3)
   #:clojure.error{:phase :execution,
                   :line 79,
                   :column 3,
                   :source "NO_SOURCE_FILE"}
             Compiler.java: 3719  clojure.lang.Compiler$InvokeExpr/eval
             Compiler.java:  457  clojure.lang.Compiler$DefExpr/eval
             Compiler.java: 7199  clojure.lang.Compiler/eval
             Compiler.java: 7149  clojure.lang.Compiler/eval
                  core.clj: 3215  clojure.core/eval
                  core.clj: 3211  clojure.core/eval
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
                  core.clj:  667  clojure.core/apply
                  core.clj: 1990  clojure.core/with-bindings*
                  core.clj: 1990  clojure.core/with-bindings*
               RestFn.java:  425  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  829  java.lang.Thread/run

1. Caused by java.lang.ClassCastException
   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')

                      REPL:   82  advent-of-clerk.year-2023.day-04/fn
                      REPL:   79  advent-of-clerk.year-2023.day-04/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
             Compiler.java: 3714  clojure.lang.Compiler$InvokeExpr/eval
             Compiler.java:  457  clojure.lang.Compiler$DefExpr/eval
             Compiler.java: 7199  clojure.lang.Compiler/eval
             Compiler.java: 7149  clojure.lang.Compiler/eval
                  core.clj: 3215  clojure.core/eval
                  core.clj: 3211  clojure.core/eval
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                  AFn.java:  152  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
                  core.clj:  667  clojure.core/apply
                  core.clj: 1990  clojure.core/with-bindings*
                  core.clj: 1990  clojure.core/with-bindings*
               RestFn.java:  425  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                  main.clj:  437  clojure.main/repl/read-eval-print/fn
                  main.clj:  437  clojure.main/repl/read-eval-print
                  main.clj:  458  clojure.main/repl/fn
                  main.clj:  458  clojure.main/repl
                  main.clj:  368  clojure.main/repl
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  829  java.lang.Thread/run

I was having a hard time spotting the problem, as I was changing a few things at once. Ultimately I realized that I've misplaced the collection and the index arguments to nth, but the stacktrace did little to help me realize that: there is no mention of call to nth there, and the line numbers are not pointing exactly to the problematic line.

The complete example code here: https://github.com/a1exsh/advent-of-clerk/blob/cddd42fcd60da2efcda37f222666328cd37fb616/src/advent_of_clerk/year_2023/day_04.clj#L84 (this is the correct code — to get the error swap the arguments like this: (nth i untouched))

The environment where I was hitting this is evaluating the single loop form in CIDER (Emacs) with C-x C-e.

It can be readily reproduced in REPL of course, just like this:

user> (nth 0 [])
Execution error (ClassCastException) at user/eval22783 (REPL:42).
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')

2 Answers

+1 vote
by

Jira ticket CLJ-2822 was created to track this issue.

0 votes
by

Not really an answer but more of a long note. And yeah, I agree that it would be helpful if line numbers were exact and the actual problematic form or at least the function name was printed.

Somewhat ironic - I can't start your project as the README describes because Clerk gives me Unhandled java.util.concurrent.RejectedExecutionException without any useful details. But no matter, I just used a regular REPL.

The top error that you see uses an "exception prettifier" that often makes things harder to figure out, so I would recommend ditching it. IMO it's not that much help that it formats an exception in this particular way and elides some not-so-useful frames, and it's definitely unhelpful that it elides useful things.

A full exception has this line: Syntax error macroexpanding at (day_04.clj:79:3).
It's not much, and the reported line is "close enough" and not exact, but it's a detail that might've helped you with figuring things out.
In this case, the only form anywhere near close to that line that works with both a vector and a number is (nth ...), and the error talks about macroexpanding because nth has :inline metadata.

by
Hm, I'm not getting an error about macroexpanding — but isn't that related to `loop` anyway?
by
Try loading a namespace with `(nth 0 [])` as a top-level form - you'll get an exception about macroexpansion as well. Expanding the `loop` macro wouldn't have resulted in such an error because it doesn't care about the types of `nth`.
by
And note that I'm not just sending that code to a REPL - I load the whole file, which AFAICT is equivalent to requiring that namespace. I suspect that's why I do see a macroexpansion error and you do not - I also don't see it if I simply evaluate `(nth 0 [])`. It might also be a difference between different tools - I use either a vanilla `clj` REPL or the one that Cursive supplies. No clue about CIDER whatsoever.
...